Question

Quelqu'un at-il une suggestion sur une meilleure façon d'intercepter un propriétés avec le château DynamicProxy?

Plus précisément, j'ai besoin PropertyInfo que je l'interception, mais ce n'est pas directement sur le IInvocation, donc ce que je fais est:

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

Alors dans mon IInterceptor:

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

}

Était-ce utile?

La solution

En général, ce ne sont pas disponibles. Les méthodes de DynamicProxy (incl. accesseurs), et il ne se soucie pas de propriétés.

Vous pourriez optimiser ce code un peu en faisant le IOnBehalfAware intercepteurs (voir ici ) et découvrir la méthodolo-> propriété dès le départ de la cartographie.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top