听从 another question of mine 上的建议,我将那里引用的代码转换为与 PostSharp 一起使用:
属性:
[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);
}
}
仍然,我无法得到任何结果(控制台上显示“有效!”)。 PostSharp 属性中的断点表明它从未被输入。
有什么帮助吗?提前致谢。
最佳答案
PostSharp 处理编译后的 IL 二进制文件并将您想要的操作添加到用属性修饰的方法 body 中。该属性本身不会做任何事情。这就是 CLR 的工作方式。它只是将属性视为数据,而不是可执行代码。没有 running PostSharp on the compiled code ,你没有得到任何特别的东西。
关于c# - Postsharp:它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1165191/