Pregunta

¿Alguien tiene una sugerencia sobre una mejor manera de interceptar una propiedad con Castle DynamicProxy?

Específicamente, necesito la propiedad en la que estoy interceptando, pero no está directamente en la invocación, así que lo que hago es:

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

Luego en mi INTERCEPTOR:

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

}

¿Fue útil?

Solución

Generalmente esto no está disponible. DynamicProxy Intercepts Methods (incls. Getters and Setters), y no le importa las propiedades.

Puede optimizar un poco este código haciendo el interceptor IOnBehalfAware (ver aquí) y descubriendo el método-> Mapeo de propiedades por adelantado.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top