Postsharp: how does it work?
Asked Answered
T

2

4

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.

Tiltyard answered 22/7, 2009 at 13:19 Comment(2)
can we get an example of the use of your attribute? Also, have you checked for compilation warnings?Sen
Hi Chris. No Warnings. Plus, the code example is already there, I think.Tiltyard
P
11

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.

Polychromy answered 22/7, 2009 at 13:23 Comment(2)
On the PostSharp tutorial on CodeProject this bit of code it seems to to exacly what I want, only trace.write instead of console.write. it doesn't work for me, anyway, neither with trace.write. codeproject.com/KB/cs/ps-custom-attributes-1.aspxTiltyard
Are you running PostSharp on your binary after compilation?Polychromy
S
3

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.

Supplant answered 22/7, 2009 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.