有没有人有更好的方法用城堡DynamicProxy拦截属性的建议?
具体地说,我需要截取的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;}
发布于 2010-06-03 07:17:53
通常这是不可用的。DynamicProxy截取方法(包括getters和setter),并且它不关心属性。
您可以通过创建拦截器方法(请参阅here)并预先发现IOnBehalfAware ->属性映射来优化此代码。
https://stackoverflow.com/questions/2959812
复制相似问题