2010-06-02 7 views
3

Quelqu'un at-il une suggestion sur une meilleure façon d'intercepter une propriété avec Castle DynamicProxy? Specifcally, j'ai besoin PropertyInfo que je l'interception, mais ce n'est pas directement sur le IInvocation, donc ce que je fais est:Propriétés d'interception avec Castle Windsor IInceptceptor

 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(); 
     } 
    } 

Puis dans mon IInterceptor:

#region IInterceptor Members 

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

    } 

    #endregion 

Merci.

Répondre

2

Généralement, ce n'est pas disponible. DynamicProxy intercepte les méthodes (y compris les getters et les setters) et ne se soucie pas des propriétés.

Vous pouvez optimiser ce code un peu en rendant l'intercepteur IOnBehalfAware (voir here) et en découvrant la propriété method-> mapping en amont.

Questions connexes