SharpCrafters Forum – Need to wrap a closure around a method..

SharpCrafters Support Forum

        


Need to wrap a closure around a method.. Expand / Collapse
Author
Message
Posted 4/8/2009 10:52:52 AM


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
Post #3599
Posted 4/9/2009 4:01:00 PM


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;) -->.
Post #3600
Posted 5/2/2009 6:12:57 AM


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
Post #3601
Posted 5/2/2009 7:23:55 AM


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.
Post #3602
« Prev Topic | Next Topic »


All times are GMT +1:00, Time now is 11:33am

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.452. 8 queries. Compression Disabled.