示例#1
0
        public void SimpleMixin()
        {
            GeneratorContext context        = new GeneratorContext();
            SimpleMixin      mixin_instance = new SimpleMixin();

            context.AddMixinInstance(mixin_instance);

            AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();

            object proxy = _generator.CreateCustomClassProxy(
                typeof(SimpleClass), interceptor, context);

            Assert.IsNotNull(proxy);
            Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType()));

            Assert.IsFalse(interceptor.Invoked);

            ISimpleMixin mixin = proxy as ISimpleMixin;

            Assert.IsNotNull(mixin);
            Assert.AreEqual(1, mixin.DoSomething());

            Assert.IsTrue(interceptor.Invoked);
            Assert.AreSame(proxy, interceptor.proxy);
            Assert.AreSame(mixin_instance, interceptor.mixin);
        }
示例#2
0
        public void MixinSerialization()
        {
            ProxyObjectReference.ResetScope();

            GeneratorContext context = new GeneratorContext();
            SimpleMixin      mixin1  = new SimpleMixin();
            OtherMixin       mixin2  = new OtherMixin();

            context.AddMixinInstance(mixin1);
            context.AddMixinInstance(mixin2);

            object proxy = generator.CreateCustomClassProxy(
                typeof(SimpleClass), new StandardInterceptor(), context);

            Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType()));

            (proxy as SimpleClass).DoSome();

            ISimpleMixin mixin = proxy as ISimpleMixin;

            Assert.AreEqual(1, mixin.DoSomething());

            IOtherMixin other = proxy as IOtherMixin;

            Assert.AreEqual(3, other.Sum(1, 2));

            SimpleClass otherProxy = (SimpleClass)SerializeAndDeserialize(proxy);

            otherProxy.DoSome();

            mixin = otherProxy as ISimpleMixin;
            Assert.AreEqual(1, mixin.DoSomething());

            other = otherProxy as IOtherMixin;
            Assert.AreEqual(3, other.Sum(1, 2));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="kernel"></param>
        /// <param name="model"></param>
        /// <param name="constructorArguments"></param>
        /// <returns></returns>
        public override object Create(IKernel kernel, ComponentModel model, params object[] constructorArguments)
        {
            IMethodInterceptor[] interceptors     = ObtainInterceptors(kernel, model);
            IInterceptor         interceptorChain = new InterceptorChain(interceptors);

            // This is a hack to avoid unnecessary object creation
            // and unecessary delegations.
            // We supply our contracts (Interceptor and Invocation)
            // and the implementation for Invocation dispatchers
            // DynamicProxy should be able to use them as long as the
            // signatures match
            GeneratorContext context = new GeneratorContext();

            context.Interceptor         = typeof(IMethodInterceptor);
            context.Invocation          = typeof(IMethodInvocation);
            context.SameClassInvocation = typeof(DefaultMethodInvocation);
            context.InterfaceInvocation = typeof(DefaultMethodInvocation);

            CustomizeContext(context, kernel, model, constructorArguments);

            object proxy = null;

            if (model.Service.IsInterface)
            {
                Object target = Activator.CreateInstance(model.Implementation, constructorArguments);

                proxy = generator.CreateCustomProxy(CollectInterfaces(model.Service, model.Implementation),
                                                    interceptorChain, target, context);
            }
            else
            {
                proxy = generator.CreateCustomClassProxy(model.Implementation,
                                                         interceptorChain, context, constructorArguments);
            }

            CustomizeProxy(proxy, context, kernel, model);

            return(proxy);
        }