views:

33

answers:

1

I'd like the aspect to exit a method invocation based on a condition like the following:

    [AttributeUsage(AttributeTargets.Method)]
    public class IgnoreIfInactiveAttribute : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
             if (condition)
            {
                **// How can I make the method return here?**
            }
        }
    }

Any help much appreciated.

+2  A: 

Ok I figured it out myself. Here the solution for the benefit of everyone:

    [AttributeUsage(AttributeTargets.Method)] 
    public class IgnoreIfInactiveAttribute : OnMethodBoundaryAspect 
    { 
        public override void OnEntry(MethodExecutionEventArgs eventArgs) 
        { 
             if (condition) 
            { 
                eventArgs.FlowBehavior = FlowBehavior.Return;
            } 
        } 
    } 
Michael Ulmann
Exactly. You could also set the return value (eventArgs.ReturnValue).
Gael Fraiteur
Hi Gael,Does that mean that the invoked method implicitly immediately returns if I set the return value property?
Michael Ulmann
@Michael Ulmann yes.
Yuriy Faktorovich