예제 #1
0
        public void EventsAreIntercepted()
        {
            CallCountHandler fireHandler = new CallCountHandler();
            CallCountHandler addHandler  = new CallCountHandler();

            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type            proxyType = interceptor.CreateProxyType(typeof(ClassWithEvent));
            ClassWithEvent  instance  = (ClassWithEvent)Activator.CreateInstance(proxyType);
            PipelineManager manager   = new PipelineManager();

            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "add_MyEvent", addHandler);
            SetPipeline(manager, instance, "FireMyEvent", fireHandler);

            bool raised = false;

            instance.MyEvent += delegate { raised = true; };

            instance.FireMyEvent();
            instance.FireMyEvent();

            Assert.IsTrue(raised);

            Assert.AreEqual(2, fireHandler.CallCount);
            Assert.AreEqual(1, addHandler.CallCount);
        }
예제 #2
0
        public void CanInterceptProperties()
        {
            CallCountHandler getHandler = new CallCountHandler();
            CallCountHandler setHandler = new CallCountHandler();

            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(OverrideableProperies));
            OverrideableProperies instance = (OverrideableProperies)Activator.CreateInstance(proxyType);

            PipelineManager manager = new PipelineManager();

            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "get_IntProperty", getHandler);
            SetPipeline(manager, instance, "set_IntProperty", setHandler);

            instance.IntProperty = 12;
            instance.IntProperty = 15;

            int total = 0;

            for (int i = 0; i < 5; ++i)
            {
                total += instance.IntProperty;
            }

            Assert.AreEqual(5 * 15, total);

            Assert.AreEqual(5, getHandler.CallCount);
            Assert.AreEqual(2, setHandler.CallCount);
        }
        public void CanInterceptProperties()
        {
            CallCountHandler getHandler = new CallCountHandler();
            CallCountHandler setHandler = new CallCountHandler();

            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(OverrideableProperies));
            OverrideableProperies instance = (OverrideableProperies)Activator.CreateInstance(proxyType);

            PipelineManager manager = new PipelineManager();
            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "get_IntProperty", getHandler);
            SetPipeline(manager, instance, "set_IntProperty", setHandler);

            instance.IntProperty = 12;
            instance.IntProperty = 15;

            int total = 0;
            for (int i = 0; i < 5; ++i)
            {
                total += instance.IntProperty;
            }

            Assert.AreEqual(5 * 15, total);

            Assert.AreEqual(5, getHandler.CallCount);
            Assert.AreEqual(2, setHandler.CallCount);
        }
        static void VirtualMethodInterceptor(Type type)
        {
            //Virtual method interceptor
            var interceptor      = new VirtualMethodInterceptor();
            var canIntercept     = interceptor.CanIntercept(type);
            var myProxyType      = interceptor.Intercept(type, typeof(MyHandler));
            var myProxy          = Activator.CreateInstance(myProxyType) as IMyType;
            var proxy            = myProxy as IInterceptionProxy;
            var otherInterceptor = proxy.Interceptor;
            var result           = myProxy.MyMethod();

            Assert.AreEqual(20, result);
        }
예제 #5
0
        public void CanDoVirtualInterception()
        {
            //Virtual method interceptor
            var type         = typeof(MyType);
            var handlerType  = typeof(ModifyResultHandler);
            var interceptor  = new VirtualMethodInterceptor();
            var canIntercept = interceptor.CanIntercept(type);
            var myProxyType  = interceptor.Intercept(type, handlerType);
            var myProxy      = Activator.CreateInstance(myProxyType) as IMyType;
            var result       = myProxy.MyMethod();

            Assert.Equal(20, result);
        }
예제 #6
0
        public void ReflectingOverProxyTypesGivesOnlyBaseProperties()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(OverrideableProperies));

            PropertyInfo[] properties = proxyType.GetProperties();

            Assert.AreEqual(2, properties.Length);
            Assert.IsTrue(Sequence.ForAll(properties, delegate(PropertyInfo pi) { return(pi.DeclaringType == typeof(OverrideableProperies)); }));
        }
예제 #7
0
        public void CantInterceptSealedClass()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            Assert.IsFalse(interceptor.CanIntercept(typeof(CantOverride)));
        }
예제 #8
0
        public void CanInterceptBasicClass()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            Assert.IsTrue(interceptor.CanIntercept(typeof(ClassWithNoVirtuals)));
        }
		static void VirtualMethodInterceptor(Type type)
		{
			//Virtual method interceptor
			var interceptor = new VirtualMethodInterceptor();
			var canIntercept = interceptor.CanIntercept(type);
			var myProxyType = interceptor.Intercept(type, typeof(MyHandler));
			var myProxy = Activator.CreateInstance(myProxyType) as IMyType;
			var proxy = myProxy as IInterceptionProxy;
			var otherInterceptor = proxy.Interceptor;
			var result = myProxy.MyMethod();
			Assert.AreEqual(20, result);
		}
        public void CantInterceptSealedClass()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Assert.IsFalse(interceptor.CanIntercept(typeof(CantOverride)));

        }
 public void CanInterceptBasicClass()
 {
     VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
     Assert.IsTrue(interceptor.CanIntercept(typeof(ClassWithNoVirtuals)));
 }
        public void EventsAreIntercepted()
        {
            CallCountHandler fireHandler = new CallCountHandler();
            CallCountHandler addHandler = new CallCountHandler();

            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(ClassWithEvent));
            ClassWithEvent instance = (ClassWithEvent)Activator.CreateInstance(proxyType);
            PipelineManager manager = new PipelineManager();
            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "add_MyEvent", addHandler);
            SetPipeline(manager, instance, "FireMyEvent", fireHandler);


            bool raised = false;
            instance.MyEvent += delegate { raised = true; };

            instance.FireMyEvent();
            instance.FireMyEvent();

            Assert.IsTrue(raised);

            Assert.AreEqual(2, fireHandler.CallCount);
            Assert.AreEqual(1, addHandler.CallCount);

        }
        public void ReflectingOverProxyTypesGivesOnlyBaseProperties()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(OverrideableProperies));
            PropertyInfo[] properties = proxyType.GetProperties();

            Assert.AreEqual(2, properties.Length);

            Assert.IsTrue(properties.All(pi => pi.DeclaringType == typeof(OverrideableProperies)));
        }