views:

24

answers:

1

So I'm working with PostSharp to pull out boilerplate logging/exception handling code so that this:

public void doSomething()
{
    Logger.Write("Entered doSomething");

    try
    {
        // code
    }
    catch (Exception ex)
    {
        ExceptionPolicy.HandleException(ex, "Errors");
    }

    Logger.Write("Exited doSomething");
}

becomes this:

[Log]
[HandleExceptions]
public void doSomething()
{
    // code
}

but, in certain places I have code that has an instance where known recovery points exist, so it looks like:

public void doSomethig()
{
    try
    {
        // code
    }
    catch (KnownException ex)
    {
        ExceptionPolicy.HandleException(ex, "Known");
    }
    finally
    {        
        this.Recover();
    }
}

I'd like to represent this as an advice but I can't seem to get access to members of the class from the advice.

+1  A: 

Yes. To invoke a member of the target class from an aspect, you have to import this member into the aspect. See http://doc.sharpcrafters.com/postsharp/2.0/Content.aspx/PostSharp.chm/html/e2086a16-ba9e-43b6-b322-12021b6f24c8.htm.

Gael Fraiteur