Windsor i Interceptor的拦截特性
-
23-10-2019 - |
题
是否有人对使用Castle Dynamicproxy拦截属性的更好方法有建议?
具体来说,我需要我要拦截的属性,但这并不直接在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;
}
解决方案
通常,这是不可用的。 Dynamicproxy拦截方法(包括Geters和Setters),它不在乎属性。
您可以通过制作拦截器来优化此代码 IOnBehalfAware
(看 这里)并发现方法 - >属性映射。
不隶属于 StackOverflow