2010-06-02 3 views
3

Есть ли у кого-нибудь предложение о лучшем способе перехвата свойств с помощью Castle DynamicProxy? Specifcally, мне нужно 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 перехватывает методы (включая геттеры и сеттеры), и он не заботится о свойствах.

Вы можете немного оптимизировать этот код, сделав перехватчик IOnBehalfAware (см. here) и узнав об открытии метода-> свойства.

Смежные вопросы