获取访问Property的方法名并读取属性值
我想将 PostSharp 方面属性放在一个属性上,并知道从哪个方法访问属性以及当时它有什么值。 PostSharp 可以做到这一点吗?
示例应阅读
static MyClass
{
[PostSharpAtrribute]
public string OutputFormat { get; set; }
}
public void Method1
{
MyClass.Instance.OutputFormat = "1";
}
public void Method2
{
MyClass.Instance.OutputFormat = "2";
}
public void Method3
{
MyClass.Instance.OutputFormat = "3";
}
PostSharp Aspect
Method "Method1" executed, property has value OutputFormat = 1
Method "Method2" executed, property has value OutputFormat = 2
Method "Method3" executed, property has value OutputFormat = 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取当前值(在更改之前,您只需使用 Args.Value,因为 Set 尚未发生。
请参阅 http://www.sharpcrafters.com/blog/post/Day-7-Interception-Aspects-e28093-Part-1.aspx 和 http://www.sharpcrafters.com/blog/post/Day-8-Interception-Aspects-e28093-Part-2.aspx
要确定调用者,您需要遍历调用堆栈使用 StackTrace http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx
或者只是在你的方法上添加一个跟踪方面(IMO 会比反映更好)调用堆栈)http://www.sharpcrafters.com/blog/post/Day-4-OnMethodBoundaryAspect.aspx
To get the current value (before the change, you just use the Args.Value because the Set has not yet happened.
see http://www.sharpcrafters.com/blog/post/Day-7-Interception-Aspects-e28093-Part-1.aspx and http://www.sharpcrafters.com/blog/post/Day-8-Interception-Aspects-e28093-Part-2.aspx
To determine the caller you will need to traverse the callstack using StackTrace http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx
or just put a trace aspect on your methods (which IMO would be better than reflecting the call stack) http://www.sharpcrafters.com/blog/post/Day-4-OnMethodBoundaryAspect.aspx