Пример #1
0
        public object CreateProxy(object instanceToProxy, IMasterProxy masterProxy)
        {
            object proxy;
            var interfaceToProxy = GetInterfaceToProxy(instanceToProxy.GetType(), masterProxy.Configuration);
            var interceptorsOfProxy = GetInterceptors(masterProxy);
            if(interfaceToProxy.IsInterface)
            {
                proxy = _proxyGenerator.CreateInterfaceProxyWithTargetInterface(
                    interfaceToProxy,
                    instanceToProxy,
                    interceptorsOfProxy);
            }
            else
            {
                var greediestCtor = interfaceToProxy.GetConstructors().OrderBy(x => x.Parameters().Count).LastOrDefault();
                var ctorDummyArgs =  greediestCtor == null
                                     ? new object[0]
                                     : greediestCtor.Parameters()
                                       .Select(p => masterProxy.Container.GetInstance(p.ParameterType))
                                       .ToArray();

                proxy = _proxyGenerator.CreateClassProxyWithTarget(
                    interfaceToProxy,
                    instanceToProxy,
                    ctorDummyArgs,
                    interceptorsOfProxy);
            }
            return proxy;
        }
Пример #2
0
        public object CreateProxy(object instanceToProxy, IMasterProxy masterProxy)
        {
            object proxy;
            var    interfaceToProxy    = GetInterfaceToProxy(instanceToProxy.GetType(), masterProxy.Configuration);
            var    interceptorsOfProxy = GetInterceptors(masterProxy);

            if (interfaceToProxy.IsInterface)
            {
                proxy = _proxyGenerator.CreateInterfaceProxyWithTargetInterface(
                    interfaceToProxy,
                    instanceToProxy,
                    interceptorsOfProxy);
            }
            else
            {
                var greediestCtor = interfaceToProxy.GetConstructors().OrderBy(x => x.Parameters().Count).LastOrDefault();
                var ctorDummyArgs = greediestCtor == null
                                     ? new object[0]
                                     : greediestCtor.Parameters()
                                    .Select(p => masterProxy.Container.GetInstance(p.ParameterType))
                                    .ToArray();

                proxy = _proxyGenerator.CreateClassProxyWithTarget(
                    interfaceToProxy,
                    instanceToProxy,
                    ctorDummyArgs,
                    interceptorsOfProxy);
            }
            return(proxy);
        }
Пример #3
0
 private IInterceptor[] GetInterceptors(IMasterProxy masterProxy)
 {
     return new[] { masterProxy }.Concat(Enumerable
         .Range(1, masterProxy.Configuration.Interceptors.Count - 1)
         .Select(i => new PseudoInterceptor())
         .Cast<IInterceptor>())
         .ToArray();
 }
Пример #4
0
        /// <summary>
        /// Creates a proxy around an instance with pseudo (empty) interceptors.
        /// </summary>
        /// <param name="proxy">The proxy.</param>
        /// <param name="interfaceType">Type of the interface.</param>
        /// <param name="instanceToWrap">The instance to wrap.</param>
        /// <returns></returns>
        public static object CreatePseudoProxy(IMasterProxy proxy, Type interfaceType, object instanceToWrap)
        {
            var pseudoList = new IInterceptor[proxy.Configuration.Interceptors.Count];
            pseudoList[0] = proxy;

            for(var i = 1; i < pseudoList.Length; i++) {
                pseudoList[i] = new PseudoInterceptor();
            }

            return CreateProxy(interfaceType, instanceToWrap, pseudoList);
        }
Пример #5
0
        private static IInterceptor[] GetInterceptors(IMasterProxy masterProxy)
        {
            var pseudoInterceptors = Enumerable.Range(1, masterProxy.Configuration.Interceptors.Count)
                                     .Select(i => new PseudoInterceptor()).ToList();
            var lastInterceptor = pseudoInterceptors.LastOrDefault();

            var interceptors = new IInterceptor[] { masterProxy }
            .Concat(pseudoInterceptors)
            //.Cast<IInterceptor>()
            .ToArray();

            FlagLastInterceptor(lastInterceptor);

            masterProxy.ResetPseudoInterceptors = () =>
            {
                pseudoInterceptors.ForEach(pi => pi.ShouldProceed = false);
                FlagLastInterceptor(lastInterceptor);
            };

            return(interceptors);
        }
Пример #6
0
        private static IInterceptor[] GetInterceptors(IMasterProxy masterProxy)
        {
            var pseudoInterceptors = Enumerable.Range(1, masterProxy.Configuration.Interceptors.Count)
                .Select(i => new PseudoInterceptor()).ToList();
            var lastInterceptor = pseudoInterceptors.LastOrDefault();

            var interceptors = new IInterceptor[] { masterProxy }
                .Concat(pseudoInterceptors)
                //.Cast<IInterceptor>()
                .ToArray();

            FlagLastInterceptor(lastInterceptor);

            masterProxy.ResetPseudoInterceptors = () =>
                                                      {
                                                          pseudoInterceptors.ForEach(pi => pi.ShouldProceed = false);
                                                          FlagLastInterceptor(lastInterceptor);
                                                      };

            return interceptors;
        }