首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用城堡Windsor IInterceptor截取属性

使用城堡Windsor IInterceptor截取属性
EN

Stack Overflow用户
提问于 2010-06-03 00:57:59
回答 1查看 3K关注 0票数 4

有没有人有更好的方法用城堡DynamicProxy拦截属性的建议?

具体地说,我需要截取的PropertyInfo,但它不是直接在IInvocation上,所以我要做的是:

代码语言:javascript
复制
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中:

代码语言:javascript
复制
public void Intercept(IInvocation invocation)
{
    bool doSomething = invocation.Method
                                 .GetProperty()
                                 .GetCustomAttributes(true)
                                 .OfType<SomeAttribute>()
                                 .Count() > 0;

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-03 07:17:53

通常这是不可用的。DynamicProxy截取方法(包括getters和setter),并且它不关心属性。

您可以通过创建拦截器方法(请参阅here)并预先发现IOnBehalfAware ->属性映射来优化此代码。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2959812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档