Determine if interception should proceed with the method

Technical questions about PostSharp Laos.

Determine if interception should proceed with the method

Postby hansel on Fri Apr 04, 2008 5:44 pm

I am having trouble finding the right example to help me with this. I want to be able to gather more information about what I am intercepting to determine if I should proceed with before and after advice at runtime.

For example, say I have this class.

Code: Select all
public class SomeClass
{
    private bool isControlVisible = false;

    public void SomeMethod1()
    {
        Log.StartMessage("Some before message goes here");
               
        if(isControlVisible)
        {
            // do this
        }
        else
        {
            // do that
        }

        // conduct the test logic here for test 1

        Log.EndMessage("Some ending message goes here");
    }
}


I would then have an interceptor like this.

Code: Select all
public class LogMethodInterceptor : IInterceptor
{
    public void Invoke(IInvocation invocation)
    {
        // do some logic before
        Console.WriteLine("Calling Before");
       
        // determine if we should proceed or not
        invocation.Proceed();
       
        // do some logic after
        Console.WriteLine("Calling After");
    }
}


You see how I can do logic in the interceptor to determine if I should proceed or not? Can I do that in PostSharp?

The currently example I am working with is the basic one from the video. Ideally, it would be great if I could get access to the method that invokes the attribute so I can use those method variables, such as the follow.

Code: Select all
[Serializable]
public class LogMethodAttribute : OnMethodBoundaryAspect
{

    public override void OnEntry(MethodExecutionEventArgs eventArgs)
    {
        // somehow get "isVisible" value from intercepted method in SomeClass
       
        // determine if the intercepted method should proceed beyond this
        // point or not.
    }

    public override void OnExit(MethodExecutionEventArgs eventArgs)
    {
        // somehow get "isVisible" value from intercepted method in SomeClass
    }
}


Thanks for the help. Sorry if I could not find this answer in other posts or in documentation. I am having trouble searching for it because I cannot properly explain it.
hansel
 
Posts: 7
Joined: Fri Apr 04, 2008 5:38 am
Full Name: Hansel Mule

Re: Determine if interception should proceed with the method

Postby gfraiteur on Sat Apr 05, 2008 10:07 am

Hi,

If you are looking for an interceptor, you should use OnMethodInvocationAspect, which has the semantics you want.

You can acheive the same goal with OnMethodBoundaryAspect, however. You should change the property eventArgs.FlowBehavior in OnEntry. You have the opportunity to skip the method execution.

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Determine if interception should proceed with the method

Postby hansel on Tue Apr 08, 2008 12:15 am

Thanks for the help. I was able to get this to work.

Another question, the [assembly: MyMethoInvocationAspect(AttributeTargetAssemblies...]

Where is the best place to put this? For example, as a scenario, I could need logging of certain functions. Where is the best approach for me to place the [assembly...] reference? Can this be configurable in an XML file, or do I need to recompile every time I want to add method interception?

Thanks.
hansel
 
Posts: 7
Joined: Fri Apr 04, 2008 5:38 am
Full Name: Hansel Mule

Re: Determine if interception should proceed with the method

Postby gfraiteur on Tue Apr 08, 2008 10:34 am

You have to recompile everytime you change the aspects, sorry.

You currently have to add the aspects using many assembly-level custom attribute instances, but another solution is to develop a CompoundAspect that reads an XML file of your choice (but you have to develop the XML format, parser and evaluation on your own). Future versions will overcome that limitation.

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Determine if interception should proceed with the method

Postby hansel on Wed Apr 09, 2008 6:21 am

Thanks again for responding. I enjoy the framework today, and will enjoy seeing it evolve. An XML configuration option would be great for a future release. Nice work!
hansel
 
Posts: 7
Joined: Fri Apr 04, 2008 5:38 am
Full Name: Hansel Mule


Return to PostSharp Laos