Intercetta gli oggetti con il Castello di Windsor IInterceptor
-
23-10-2019 - |
Domanda
Qualcuno ha un suggerimento su un modo migliore per intercettare una proprietà con Castle DynamicProxy?
In particolare, ho bisogno del PropertyInfo che sto intercettare, ma non è direttamente sul IInvocation, quindi quello che faccio è:
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();
}
}
Poi nel mio IInterceptor:
public void Intercept(IInvocation invocation)
{
bool doSomething = invocation.Method
.GetProperty()
.GetCustomAttributes(true)
.OfType<SomeAttribute>()
.Count() > 0;
}
Soluzione
In genere questo non è disponibile. metodi DynamicProxy intercetta (incl. getter e setter), e non si preoccupa di proprietà.
Si potrebbe ottimizzare questo codice un po 'facendo il IOnBehalfAware
intercettore (vedi qui ) e scoprire il metodo-> proprietà mapping in anticipo.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow