unity 2.0 interception 学习3

上一篇:unity 2.0 interception 学习2


前面讲的是类型拦截,在类型拦截中,目标类型不是原始类型被拦截的类原始类型被拦截的类会被动态的派生,调用这个派生类的virtual方法实际会转移到behavior[0]的invoke方法。

今天看看实例拦截:

public static object ThroughProxyWithAdditionalInterfaces(
            Type interceptedType,
            object target,
            IInstanceInterceptor interceptor,
            IEnumerable<IInterceptionBehavior> interceptionBehaviors,
            IEnumerable<Type> additionalInterfaces)
        {
            Guard.ArgumentNotNull(interceptedType, "interceptedType");
            Guard.ArgumentNotNull(target, "target");
            Guard.ArgumentNotNull(interceptor, "interceptor");
            Guard.ArgumentNotNull(interceptionBehaviors, "interceptionBehaviors");
            Guard.ArgumentNotNull(additionalInterfaces, "additionalInterfaces");

            if (!interceptor.CanIntercept(interceptedType))
            {
                throw new ArgumentException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.InterceptionNotSupported,
                        interceptedType.FullName),
                    "interceptedType");
            }

            var behaviors = interceptionBehaviors.ToList();
            if(behaviors.Where(ib => ib == null).Count() > 0)
            {
                throw new ArgumentException(
                    string.Format(CultureInfo.CurrentCulture, Resources.NullBehavior),
                    "interceptionBehaviors");
            }

            var activeBehaviors = behaviors.Where(ib => ib.WillExecute).ToList();

            var allAdditionalInterfaces
                = GetAllAdditionalInterfaces(activeBehaviors, additionalInterfaces).ToList();

            // If no behaviors and no extra interfaces, nothing to do.
            if(activeBehaviors.Count == 0 && allAdditionalInterfaces.Count == 0)
            {
                return target;
            }

            IInterceptingProxy proxy =
                interceptor.CreateProxy(interceptedType, target, allAdditionalInterfaces.ToArray());

            foreach (IInterceptionBehavior interceptionBehavior in activeBehaviors)
            {
                proxy.AddInterceptionBehavior(interceptionBehavior);
            }

            return proxy;
        }

一、InterfaceInterceptor:

public IInterceptingProxy CreateProxy(Type t, object target, params Type[] additionalInterfaces)
        {
            Guard.ArgumentNotNull(t, "t");
            Guard.ArgumentNotNull(additionalInterfaces, "additionalInterfaces");

            Type interceptorType;
            Type typeToProxy = t;
            bool genericType = false;

            if (t.IsGenericType)
            {
                typeToProxy = t.GetGenericTypeDefinition();
                genericType = true;
            }

            GeneratedTypeKey key = new GeneratedTypeKey(typeToProxy, additionalInterfaces);
            lock (interceptorClasses)
            {
                if (!interceptorClasses.TryGetValue(key, out interceptorType))
                {
                    InterfaceInterceptorClassGenerator generator =
                        new InterfaceInterceptorClassGenerator(typeToProxy, additionalInterfaces);
                    interceptorType = generator.CreateProxyType();
                    interceptorClasses[key] = interceptorType;
                }
            }

            if (genericType)
            {
                interceptorType = interceptorType.MakeGenericType(t.GetGenericArguments());
            }
            return (IInterceptingProxy)interceptorType.GetConstructors()[0].Invoke(new object[] { target, t });
        }

我的目标是:为什么代理实例对方法的调用会转到target的调用上,即如何实现类似装饰模式的


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值