Search code examples
c#.netpostsharp

How to create an aspect checking for null references on all methods in a class in postsharp


How to create an aspect checking for null references on all methods in a class in postsharp.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    [MethodParameterNullCheck]
    internal class Class
    {
        public Class()
        {

        }

        public void MethodA(int i, ClassA a, ClassB b)
        {
              //Some business logic
        }
    }
}

The aspect [MethodParameterNullCheck] should then unfold to the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    [MethodParameterNullCheck]
    internal class Class
    {
        public Class()
        {

        }

        public void MethodA(int i, ClassA a, ClassB b)
        {
            if (a == null) throw new ArgumentNullException("Class->MethodA: Argument a of ClassA is not allowed to be null.");
            if (b == null) throw new ArgumentNullException("Class->MethodA: Argument b of ClassB is not allowed to be null.");
            // Some Business Logic
        }
    }
}

I will appreciate if you can give me a sample implementation on this to get me startet on AOP with postsharp.


Solution

  • An alternative approach is an extension method:

    public static void ThrowIfNull<T>(this T obj, string parameterName) where T : class
    {
        if(obj == null) throw new ArgumentNullException(parameterName);
    }
    

    then call:

    foo.ThrowIfNull("foo");
    bar.ThrowIfNull("bar");
    

    The T : class pervents us accidentally boxing ints etc.

    Re AOP; Jon Skeet has a sample for something similar here - but covering a single method/parameter.

    Here's the aspect reproduced; note that this aspect covers only 1 argument at a time, and is method-specific, but in general I'd argue that this is perfectly reasonable... however, you could probably change it.

    using System;
    using System.Reflection;
    using PostSharp.Laos;
    
    namespace IteratorBlocks
    {
        [Serializable]
        class NullArgumentAspect : OnMethodBoundaryAspect
        {
            string name;
            int position;
    
            public NullArgumentAspect(string name)
            {
                this.name = name;
            }
    
            public override void CompileTimeInitialize(MethodBase method)
            {
                base.CompileTimeInitialize(method);
                ParameterInfo[] parameters = method.GetParameters();
                for (int index = 0; index < parameters.Length; index++)
                {
                    if (parameters[index].Name == name)
                    {
                        position = index;
                        return;
                    }
                }
                throw new ArgumentException("No parameter with name " + name);
            }
    
            public override void OnEntry(MethodExecutionEventArgs eventArgs)
            {
                if (eventArgs.GetArguments()[position] == null)
                {
                    throw new ArgumentNullException(name);
                }
            }
        }
    }