【问题标题】:StructureMap with Interception and Castle.DynamicProxy带有拦截和 Castle.DynamicProxy 的 StructureMap
【发布时间】:2010-05-07 14:20:07
【问题描述】:

我试图让 StructureMap 在它创建的一些对象周围放置一个 Castle.DynamicProxy。我之前使用过 EnrichWith 功能,但我认为 RegisterInterception 在这种情况下更适合我,因为我使用了扫描。

问题在于,在“Process(object target, IContext context)”方法中,我无法找出 SM 试图获取的接口,只有具体的类。我可以找到这个类实现的所有接口,但如果它实现了多个接口,我不知道如何找到实际请求的接口。有没有办法做到这一点?

这里有一些代码:

    public class SMInterceptor : TypeInterceptor
    {
        private readonly IInterceptor _interceptor;
        private readonly ProxyGenerator _proxyGenerator;


        public SMInterceptor(IInterceptor interceptor, ProxyGenerator proxyGenerator)
        {
            _interceptor = interceptor;
            _proxyGenerator = proxyGenerator;
        }

        public static List<Type> TypesToIntercept = new List<Type>();

        public object Process(object target, IContext context)
        {
            var interfaceToTarget = // This is where I want the target interface!
            var decorator = _proxyGenerator.CreateInterfaceProxyWithTarget(interfaceToTarget, target, _interceptor);
            return decorator;
        }

        public bool MatchesType(Type type)
        {
           return true;
        }
   }

【问题讨论】:

    标签: .net structuremap aop ioc-container


    【解决方案1】:

    有点晚了,但下面的代码应该可以工作(假设请求的类型是实例)

    var interfaceToTarget = context.BuildStack.Current.RequestedType;
    

    【讨论】:

    • 这不太对劲。我在这里做了一个简单的控制台应用程序示例:pastie.org/1088333 这将返回:接口:ConsoleApplication1.IOne 目标:ConsoleApplication1.Two 而我真正想要的是这不太正确。我在这里做了一个简单的控制台应用程序示例:pastie.org/1088333 这返回:接口:ConsoleApplication1.ITwo
    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 2018-07-04
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    相关资源
    最近更新 更多