2011-10-04 2 views
0

하나의 속성에 PostSharp aspect 특성을 넣으 려하고 어떤 메서드 속성이 액세스되었으며 그 시점에서 어떤 값을 갖고 있는지 알고 싶습니다. 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의 화면은

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

는 (설정이 아직 일어나지 않았기 때문에 변경하기 전에, 당신은 단지 Args.Value을 사용하여 현재 값을 얻으려면 읽어야합니다.

[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

당신이

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

을 스택 트레이스 http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx를 사용하여 호출 스택을 통과하거나 http://www.sharpcrafters.com/blog/post/Day-4-OnMethodBoundaryAspect.aspx

+0

(IMO 호출 스택을 반영하는 것보다 더 좋을 것이다) 당신의 방법에 추적 측면을 넣어해야합니다 발신자를 확인하려면 "방법에 대한 추적 측면"은 필자가 필요로하는 것이지만 측면간에 데이터를 공유하는 방법입니다. 메서드 항목을 가져 오려면 OnMethodBoundaryAspect가 사용되고 LocationInterceptionAspect aspect의 속성 값을 읽습니다. 따라서 LocationInterceptionAspect aspect에서 OnMethodBoundaryAspect aspect에 마지막 엔트리가 무엇인지 찾아야 할 것입니다. – Tomas

+0

복잡한 측면을 구축하는 데 필요한 측면간에 데이터를 공유합니다. 원하는 것을 정확하게하는 것에 대한 자세한 내용은 http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-13-e28093-Aspect-Providers-e28093-Part-2.aspx를 참조하십시오. –

관련 문제