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.
An alternative approach is an extension method:
then call:
The
T : classpervents 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.