예제 #1
0
        public void GeneratedProxyCallsInterceptionBehaviors()
        {
            IInstanceInterceptor   interceptor = new InterfaceInterceptor();
            ImplementsInterfaceOne target      = new ImplementsInterfaceOne();

            IInterceptingProxy            proxy = interceptor.CreateProxy(typeof(IInterfaceOne), target);
            CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();

            proxy.AddInterceptionBehavior(interceptionBehavior);

            IInterfaceOne intercepted = (IInterfaceOne)proxy;

            intercepted.TargetMethod();
            intercepted.TargetMethod();
            intercepted.TargetMethod();

            Assert.AreEqual(3, interceptionBehavior.CallCount);
        }
예제 #2
0
        public void GeneratedProxyCallsHandlers()
        {
            IInstanceInterceptor   interceptor = new InterfaceInterceptor();
            ImplementsInterfaceOne target      = new ImplementsInterfaceOne();

            IInterceptingProxy proxy   = interceptor.CreateProxy(typeof(IInterfaceOne), target);
            CallCountHandler   handler = new CallCountHandler();

            proxy.SetPipeline(typeof(IInterfaceOne).GetMethod("TargetMethod"),
                              new HandlerPipeline(new ICallHandler[] { handler }));

            IInterfaceOne intercepted = (IInterfaceOne)proxy;

            intercepted.TargetMethod();
            intercepted.TargetMethod();
            intercepted.TargetMethod();

            Assert.AreEqual(3, handler.CallCount);
        }
예제 #3
0
        public void InterceptorCreatesProxyInstance()
        {
            IInstanceInterceptor   interceptor = new InterfaceInterceptor();
            ImplementsInterfaceOne target      = new ImplementsInterfaceOne();

            IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IInterfaceOne), target);

            IInterfaceOne intercepted = (IInterfaceOne)proxy;

            intercepted.TargetMethod();

            Assert.IsTrue(target.TargetMethodCalled);
        }
예제 #4
0
            public void TargetMethod()
            {
                MethodInfo targetMethod = typeof(IInterfaceOne).GetMethod("TargetMethod");

                VirtualMethodInvocation input         = new VirtualMethodInvocation(target, targetMethod);
                HandlerPipeline         pipeline      = ((IInterceptingProxy)this).GetPipeline(targetMethod);
                IMethodReturn           returnMessage = pipeline.Invoke(input, delegate(IMethodInvocation inputs, GetNextHandlerDelegate getNext)
                {
                    try
                    {
                        target.TargetMethod();
                        return(inputs.CreateMethodReturn(null));
                    }
                    catch (Exception ex)
                    {
                        return(inputs.CreateExceptionMethodReturn(ex));
                    }
                });

                if (returnMessage.Exception != null)
                {
                    throw returnMessage.Exception;
                }
            }