Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

aop

postsharp

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.

like image 463
Michael Ulmann Avatar asked Mar 13 '10 08:03

Michael Ulmann


1 Answers

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;
            } 
        } 
    } 
like image 199
Michael Ulmann Avatar answered Nov 15 '22 05:11

Michael Ulmann