2010-06-02 122 views
3

有沒有人有更好的方法來攔截與Castle DynamicProxy屬性的建議?比較特別的,我需要的是我截取的PropertyInfo,但它不是直接在IInvocation,所以我要做的就是:截取屬性與城堡溫莎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(); 
     } 
    } 

然後在我的IInterceptor:

#region IInterceptor Members 

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

    } 

    #endregion 

感謝。

回答

2

通常這不可用。 DynamicProxy攔截方法(包括getters和setter),它不關心屬性。

您可以通過製作攔截器IOnBehalfAware(請參閱here)並發現方法 - >屬性映射的前沿來稍微優化此代碼。