3
有沒有人有更好的方法來攔截與Castle DynamicProxy屬性的建議?比較特別的,我需要的是我截取的PropertyInfo,但它不是直接在IInvocation,所以我要做的就是:截取屬性與城堡溫莎IInterceptor
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:
#region IInterceptor Members
public void Intercept(IInvocation invocation)
{
bool doSomething = invocation.Method.GetProperty().GetCustomAttributes(true).OfType<SomeAttribute>().Count() > 0;
}
#endregion
感謝。