Reputation: 6611
Before I start, I'd like to clarify that my current understanding of AOP terminology is as follows...
Onto the actual question...
I have a logging aspect in PostSharp which I want to use (weave) on every method, excluding properties. Originally I was using the following on my aspect:
[MulticastAttributeUsage(MulticastTargets.Method, TargetMemberAttributes = MulticastAttributes.Instance)]
However, I found the aspect was still being woven into properties, meaning I had to perform a secondary check at runtime to preclude my code from executing on properties:
if (!methodName.StartsWith("set_") && !methodName.StartsWith("get_")) {
This is not ideal. I should be able to define this behavior in my pointcut so that I don't have to perform any runtime checking.
I have been looking into the MethodPointcut
attribute which appears to provide me with a callback to help the weaver select candidates for the advice at build time. Please can I see an example?
Assuming this does work, I am still left thinking 'Why must I hardcode Pointcuts to my Advices?'. Aspects and Advices are the definition/implementation. Pointcuts are the usage. The two should be seperate.
Upvotes: 2
Views: 1406
Reputation: 14585
Properties ARE methods, two methods to be exact. Remember, PostSharp does it's work AFTER MSBuild turns your C# into MSIL.
What you do is not check at runtime but check at compile time. Override the CompiletimeValidate() method and move your check code there. If the method matches, return false other wise return true. PostSharp uses this method to determine (at compile time) if the aspect will be applied to the target. Nothing has to happen at runtime.
You can also do this using multicasting. See the following PostSharp Principals article for detials on that http://www.sharpcrafters.com/blog/post/Day-3-Applying-Aspects-with-Multicasting-Part-2.aspx
[assembly: MyAspect(AspectPriority = 10)]
[assembly: MyAspect(AspectPriority = 0,
AttributeExclude = true, AttributeTargetMembers = "regex:get_.*|set_.*")]
MethodPointcut (along with a few others) is meant for complex aspects. There will be an article coming out about these next week.
Upvotes: 3