【问题标题】:Castle IInterceptorSelector and Implementation Type MethodCastle IInterceptorSelector 和实现类型方法
【发布时间】:2015-05-04 04:09:35
【问题描述】:

您好,我创建了一个用于重试支持的拦截器,并使用 RetryAttribute 标记方法。

我已经实现了一个 IInterceptorSelector,仅当类型具有具有该自定义属性的方法时才返回 Interceptor。

在 RetryInterceptor 类中,我在调用提供的方法上查找该属性,如果存在,则运行重试逻辑。

我希望 IInterceptorSelector 根据方法过滤拦截器集,但传入的方法是其他接口类型,而不是实现它的实际类。有没有办法做到这一点?

谢谢。

【问题讨论】:

  • 你能分享一些概述问题的代码吗?

标签: c# inversion-of-control castle-windsor aop interceptor


【解决方案1】:

您可以使用接口方法信息简单地从类型中检查方法信息:

public class Selector : IInterceptorSelector
{
    public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
    {
        var attributes = type.GetMethod(method.Name).GetCustomAttributes(false);
        if (attributes.OfType<Retry>().Any())
        {
            // return retry interceptor
        }
        else
        {
            // return no interceptor
        }
    }
}

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多