Postsharp: how does it work?

Following the tips I got on another issue , I converted the code that is quoted for use 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); } } 

However, I cannot get any result ("Works!" On the console). The breakpoint in the PostSharp attribute indicates that it is never entered.

Any help? Thanks in advance.

+4
source share
2 answers

PostSharp processes the compiled IL file and adds the action you want to the body method decorated with the attribute. The attribute will not do anything on its own. Here's how the CLR works. It simply treats attributes as data, not executable code. Without running PostSharp on compiled code , you won’t get anything special.

+10
source

You do not need to run the PostSharp command-line utility, but you need to install it correctly.

The easiest way is to install PostSharp using the installer.

Otherwise, you must edit the project file using a text editor as described in the documentation .

+3
source

All Articles