catch and finally blocks from separated advices

Do you program directly PostSharp.CodeModel or another low-level API and need help? Put your questions here and we will try to answer.

catch and finally blocks from separated advices

Postby Ed_ward on Sun Feb 10, 2008 9:14 pm

Hi Gael.

Could you help me with another one problem?

I have two independent advices, one of them uses BeforeMethodBody and AfterMethodBodyAlways JoinPointKinds, hence creates a finally block. Another one advice uses AfterMethodBodyException JoinPointKinds, hence creates a catch block.

Result of their work are nested instructions blocks, e.g.
Code: Select all
try
    {
        // result of work of the first advice
        IDisposable CS$9861e9;
        try
        {
            CS$9861e9 = this._logbook.GetTrace(this);
            throw new NotImplementedException(); // except this line
        }
        finally
        {
            if (CS$9861e9 != null)
            {
                CS$9861e9.Dispose();
            }
        }
    }
    // this catch block (and the first "try") is the result of work of the second advice
    catch (Exception CS$678028)
    {
        if (!MainForm_ExceptionsHandler.Handle(this, CS$678028))
        {
            throw;
        }
    }


Is it possible to make code like this (finally and catch block are siblings) as the result of work of two advices?
Code: Select all
try
{
   // result of work of the first advice
    IDisposable CS$9861e9;
   
    CS$9861e9 = this._logbook.GetTrace(this);
    throw new NotImplementedException(); // except this line
}
// this catch blockis the result of work of the second advice
catch (Exception CS$678028)
{
     if (!MainForm_ExceptionsHandler.Handle(this, CS$678028))
     {
         throw;
     }
}
finally
{
     if (CS$9861e9 != null)
    {
         CS$9861e9.Dispose();
    }
}

thanks in advance

Ed.ward
Last edited by gfraiteur on Mon Feb 11, 2008 3:22 pm, edited 1 time in total.
Reason: formatting code so I can read it
Edward Pavlov
Ed_ward
 
Posts: 11
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Edward Pavlov

Re: catch and finally blocks from separated advices

Postby gfraiteur on Mon Feb 11, 2008 3:24 pm

It is just a matter of aspect priority. Play with IAdvice.Priority: the advice with the lesser value will be the most external block.

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: catch and finally blocks from separated advices

Postby Ed_ward on Mon Feb 11, 2008 3:42 pm

I see, but in this case I want two advices to generate code at the same level.

The first advice must generate catch block, the second one must generate finally block, and these blocks must be on the same level.

Is it possible or no?

Ed.ward
Edward Pavlov
Ed_ward
 
Posts: 11
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Edward Pavlov

Re: catch and finally blocks from separated advices

Postby gfraiteur on Mon Feb 11, 2008 3:44 pm

No, it is not possible.
You have to make two advices and give them different priorities.

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


Return to PostSharp Core