2010-06-02 7 views
3

Castle DynamicProxy로 속성을 가로채는 더 좋은 방법에 대한 제안이 있습니까? 내 IInterceptor에서 다음캐슬 윈저 IInterceptor로 가로 채기 속성

 public static PropertyInfo GetProperty(this MethodInfo method) 
    { 
     bool takesArg = method.GetParameters().Length == 1; 
     bool hasReturn = method.ReturnType != typeof(void); 
     if (takesArg == hasReturn) return null; 
     if (takesArg) 
     { 
      return method.DeclaringType.GetProperties() 
       .Where(prop => prop.GetSetMethod() == method).FirstOrDefault(); 
     } 
     else 
     { 
      return method.DeclaringType.GetProperties() 
       .Where(prop => prop.GetGetMethod() == method).FirstOrDefault(); 
     } 
    } 

:

#region IInterceptor Members 

    public void Intercept(IInvocation invocation) 
    { 
     bool doSomething =         invocation.Method.GetProperty().GetCustomAttributes(true).OfType<SomeAttribute>().Count() > 0; 

    } 

    #endregion 

덕분에 내가 무엇이며, 특별한 방법, 나는 차단있어 PropertyInfo이 필요하지만, 그것은 IInvocation에 직접 아니다.

답변

2

일반적으로 사용할 수 없습니다. DynamicProxy는 메서드 (getter 및 setters 포함)를 가로 채며 속성을 신경 쓰지 않습니다.

인터셉터를 IOnBehalfAware (here 참조)으로 만들고 메소드 -> 속성 매핑을 앞에서 발견하면이 코드를 약간 최적화 할 수 있습니다.