获取访问Property的方法名并读取属性值

发布于 2024-12-07 17:10:48 字数 623 浏览 1 评论 0 原文

我想将 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

I would like to put PostSharp aspect attribute on one Property and know from which method property was accessed and what value it had at that time. Is that possible with PostSharp?

Example

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 should read

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2024-12-14 17:10:48

要获取当前值(在更改之前,您只需使用 Args.Value,因为 Set 尚未发生。

[Serializable] 
public class MyPropertyAspect: LocationInterceptionAspect 
{ 
    public override void OnSetValue(LocationInterceptionArgs args) 
    { 
            object current = args.Value; //Set has not happened, remember this is an interception
            args.ProceedSetValue(); 

    }           
}

请参阅 http://www.sharpcrafters.com/blog/post/Day-7-Interception-Aspects-e28093-Part-1.aspxhttp://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

var st = new StackTrace();
st.GetFrame(1).GetMethod().Name; //Might also be frame 2

或者只是在你的方法上添加一个跟踪方面(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.

[Serializable] 
public class MyPropertyAspect: LocationInterceptionAspect 
{ 
    public override void OnSetValue(LocationInterceptionArgs args) 
    { 
            object current = args.Value; //Set has not happened, remember this is an interception
            args.ProceedSetValue(); 

    }           
}

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

var st = new StackTrace();
st.GetFrame(1).GetMethod().Name; //Might also be frame 2

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文