Перехватывание свойств с Castle Windsor Iinterceptor
-
23-10-2019 - |
Вопрос
У кого -нибудь есть предложение о лучшем способе перехватить свойства с динамикой замка?
В частности, мне нужна PropertyInfo, которое я перехватываю, но это не напрямую на iinvocation, так что я делаю:
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:
public void Intercept(IInvocation invocation)
{
bool doSomething = invocation.Method
.GetProperty()
.GetCustomAttributes(true)
.OfType<SomeAttribute>()
.Count() > 0;
}
Решение
Обычно это недоступно. Динамические методы перехвата (в том числе и сеттеры), и это не заботится о свойствах.
Вы можете немного оптимизировать этот код, сделав перехватчик IOnBehalfAware
(видеть здесь) и обнаружение метода-> отображения свойств заранее.
Не связан с StackOverflow