我想将PostSharp aspect属性放在一个Property上,并知道访问哪个方法属性以及它当时具有什么值。这可能与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
答案 0 :(得分:0)
要获取当前值(在更改之前,您只需使用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.aspx和http://www.sharpcrafters.com/blog/post/Day-8-Interception-Aspects-e28093-Part-2.aspx
要确定调用者,您需要使用StackTrace遍历callstack 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