|
|
|
Community Member
|
|
Hi,
I'm looking at using PostSharp to wrap a closure around an attribute decorated method. Something like:
[WrapThisWithMyClosure] public void foo() { // do something }
so this I get the following:
[WrapThisWithMyClosure] public void foo() { while(someCondition) { // do something } }
My understanding is that I need something which is a cross between OnExceptionAspect and OnMethodBoundaryAspect...
Anyone got any suggestions before I dive in and write my own weaver?
cheers
Tom
|
|
|
|
|
Gael Fraiteur
SharpCrafters
|
|
You need to develop your own abstract aspect using PostSharp Core. Your aspect will provide two low-level advices: on entry and on success. You will evaluate the condition on OnEntry and jump from OnSuccess to OnEntry. I'm not that sure that it will be possible to implement the jump from OnEntry to OnSuccess.
Jumping from OnEntry to OnSuccess may be tricky, because, when you implement OnEntry, you don't have the goto target (it will be generated in OnSuccess). The trick is simply to create the goto target (InstructionSequence) of OnSuccess already in OnEntry so you can emit the goto, then, in OnEntry, add this InstructionSequence to the InstructionBlock you get, and emit instructions into it.
So this is all possible, although it breaks the principle that advices of the low-level weaver should not branch out of their instruction block. Except this theoretical issue, it should work.
Good luck!
-gael
PS. You would better hire me to develop it <!-- s;) --> <!-- s;) -->.
|
|
|
|
|
Community Member
|
|
Tom-
If I understand this correctly, you could use the OnMethodInvocationAspect base class. Inside OnInvocation, test your condition, then call eventArgs.Proceed(). Wrap the whole thing in a try/catch.
-Chris
|
|
|
|
|
Gael Fraiteur
SharpCrafters
|
|
I could add a new feature in PostSharp 2.0, allowing to loop by setting:
eventArgs.FlowBehavior = FlowBehavior.Restart
This would be pretty simple to implement.
|
|
|
|