Today we jump face first into the decompiled code of interception aspects. If you have not yet read the previous post (part 1), then I suggest doing so before continuing. We’re going to look at the IoCResolution aspect we built yesterday and finish off with chaining.
Under the hood
Using ILSpy, open the PostSharpDemo1.exe and browse to the contactRepository field under ContactManager.
We see a getter and a setter even though this was a field and not a property. The setter contains the standard code to set the backing to the value passed in. There is no change since we did not implement OnSetValue in our aspect.
The getter starts off by instantiating a generic instance of LocationInterceptionArgs that will be passed to our aspect’s OnGetValue method. Next it sets the Location property to the LocationInfo held in <>z_Aspects.l7. We saw this in the MethodInterceptionAspect except it was a MethodInfo type instead of LocationInfo. <>z_Aspects.l7 is specifically for the ContactManager.contactRepository field. TypeBinding is then set to the singleton instance of the binding PostSharp has created for contactRepository, ContactManager.<contactRepository>c_Binding.
The TypeBinding is necessary because the LocationInterceptionArgs contain the ProceedSetValue, ProceedGetValue, GetCurrentValue and SetNewValue methods which use the binding to access the field. We use these methods to manipulate the field/property from our aspect.
Finally, our aspect’s OnGetValue method is called passing in the LocationInterceptionArgs and then the value to returned using the LocationInterceptionArgs.TypedValue. TypedValue will always be of the generic type used to instantiate LocationInterceptionArgs.
Chaining
When it comes to interception aspects, we have to remember how they work because when applying multiple interception aspects to the same target, a chain is created. Each interception aspect is a node on the invocation chain which means you may or may not be invoking the actual target when proceeding. If we were to apply both a location interception aspect and a method interception aspect to a single target, the chain would become
Each aspect intercepts the preceding aspect. The original call ends up invoking the last applied aspect. It’s important to understand this when applying aspects that change the invocation as many things can be affected such as performance and behavior.
Note that the ordering of the MethodInterceptionAspect and the LocationInterceptionAspect may be inversed. We’ll see another day how to specify the order of application of several aspects when they are applied to the same element of code.
Conclusion
It may seem overwhelming at first, but once you understand how PostSharp produces its results it’s easy to grasp what’s going on when troubleshooting. Thankfully PostSharp is a mature framework and there are little, if any, side effects.
![self573_thumb[1] self573_thumb[1]](/assets/images/2011/2011-07-06-day_8_interception_aspects_e28093_part_2/self573_thumb[1]_thumb_1.jpg)