views:

122

answers:

2

PostSharp gives this error:

PostSharp: Cannot apply an OnMethodInvocation aspect (...) with target-site weaving on the abstract or external method "...". Consider excluding the aspect from this method or use call-site weaving.

Suggested call-site weaving is not an appropriate solution for me. Is there any way to add pre/post processing for the abstract method?

A: 

This is of course imposible - how would you perform target-site weaving with an abstract method, a method without any code? You can of course perform target-site weaving with all derived types implementing the method, but this requires that you control this types. Or you could make the method non-abstract and virtual and require derived types to call the base implementation.

Daniel Brückner
There is at least one way to make it possible - use "template method" pattern. PostSharp already uses it for virtual methods. The same can be done with abstract methods.
skevar7
"[...] way to add pre/post processing for the abstract method?" Do you mean performing some steps before or after a call in code? And what do you want to achieve with the aspect?
Daniel Brückner
I just need to perform some code before and after each method from the specified group of methods. PostSharp seemed a good solution, but problem with abstract methods makes it impossible.
skevar7
+1  A: 

I suggest you use aspect inheritance. The aspect will be automatically added to all methods implementing the abstract method.

See MulticastAttribute.AttributeInheritance or MulticastAttributeUsageAttribute.Inheritance, or official documentation for details.

Gael Fraiteur
Looks like what I need, but it doesn't work correctly with OnMethodInvocation aspect. Any ideas? BTW, your documentation behaves very oddly in Opera browser.
skevar7
OnMethodInvocation should work even with inheritance. Check with Reflector to understand what happens.
Gael Fraiteur
It shows compile-time error for this aspect ;)
skevar7