How to exit a method in the OnEntry method of a PostSharp aspect based on condition
Asked Answered
C

1

5

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.

Consistent answered 13/3, 2010 at 8:48 Comment(0)
C
10

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;
            } 
        } 
    } 
Consistent answered 13/3, 2010 at 8:55 Comment(2)
Exactly. You could also set the return value (eventArgs.ReturnValue).Siler
Hi Gael, Does that mean that the invoked method implicitly immediately returns if I set the return value property?Consistent

© 2022 - 2024 — McMap. All rights reserved.