Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postsharp: how does it work?

Following the advice got on another question of mine, I converted the code there quoted to be used with PostSharp:

Attribute:

[Serializable]
public sealed class InitAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionEventArgs eventArgs)
    {
        Console.Write("Works!");
    }
}


static class Logger
{
    public static string _severity;

    public static void Init(string severity)
    {
        _severity = severity;
    }

    [Init()]
    public static void p()
    {
        Console.WriteLine(_severity);
    }
}

Still, I cannot get any result ("Works!" on the console). A breakpoint within the PostSharp attribute reveals that it is never entered.

Any Help? Thanks in advance.

like image 666
pistacchio Avatar asked Dec 09 '22 20:12

pistacchio


2 Answers

PostSharp processes the compiled IL binary and adds action you want to the method body decorated with the attribute. The attribute won't do anything by itself. This is how CLR tends to work. It just treats attributes as data, not executable code. Without running PostSharp on the compiled code, you don't get anything special.

like image 131
mmx Avatar answered Dec 27 '22 09:12

mmx


You do not need to execute the PostSharp command-line utility, but you need to install properly.

The easiest way is to install PostSharp using the installer.

Otherwise, you should edit your project file using a text editor as described in documentation.

like image 43
Gael Fraiteur Avatar answered Dec 27 '22 07:12

Gael Fraiteur