[英]Intercept Properties With Castle Windsor IInterceptor
有沒有人建議用更好的方法攔截Castle 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;
}
通常這是不可用的。 DynamicProxy攔截方法(包括getter和setter),它不關心屬性。
您可以通過制作攔截器IOnBehalfAware
(請參閱此處 )並預先發現method->屬性映射來優化此代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.