public void Unproxied_methods_should_pass_through_to_target()
        {
            var target = new HasVirtualStringAutoProperty();

            var options = new ProxyGenerationOptions(
                hook: new ProxySomeMethodsHook(
                    shouldInterceptMethod: (_, method) =>
                    method.Name == "set_" + nameof(HasVirtualStringAutoProperty.Property)
                    )
                );

            var convertToLowerThenProceed = new WithCallbackInterceptor(
                invocation =>
            {
                string value     = (string)invocation.GetArgumentValue(0);
                string lowerCase = value?.ToLowerInvariant();
                invocation.SetArgumentValue(0, lowerCase);
                invocation.Proceed();
            }
                );

            var proxy = generator.CreateClassProxyWithTarget(
                target,
                options,
                convertToLowerThenProceed
                );

            proxy.Property = "HELLO WORLD";

            Assert.AreEqual("hello world", target.Property);
            Assert.AreEqual("hello world", proxy.Property);
        }
Example #2
0
        public void CanCallMethodWithOutParameter()
        {
            int i;
            var interceptor = new WithCallbackInterceptor(delegate { });
            var proxy       = (IWithRefOut)generator.CreateInterfaceProxyWithoutTarget(typeof(IWithRefOut), interceptor);

            proxy.Do(out i);
        }
Example #3
0
        public void CanAffectValueOfOutParameter()
        {
            int i;
            var interceptor =
                new WithCallbackInterceptor(delegate(IInvocation invocation) { invocation.Arguments[0] = 5; });
            var proxy = (IWithRefOut)generator.CreateInterfaceProxyWithoutTarget(typeof(IWithRefOut), interceptor);

            proxy.Do(out i);
            Assert.AreEqual(5, i);
        }
Example #4
0
        public void CanCreateProxyWithRefParam()
        {
            var i           = 3;
            var interceptor =
                new WithCallbackInterceptor(delegate(IInvocation invocation) { invocation.Arguments[0] = 5; });
            var proxy = (IWithRefOut)generator.CreateInterfaceProxyWithoutTarget(typeof(IWithRefOut), interceptor);

            proxy.Did(ref i);
            Assert.AreEqual(5, i);
        }
		public void Same_Interface_on_proxy_withouth_target_and_mixin_should_forward_to_null_target()
		{
			var interceptor = new WithCallbackInterceptor(i =>
			                                              	{
			                                              		Assert.IsNull(i.InvocationTarget);
			                                              		i.ReturnValue = 0;
			                                              	});
			var mixin = new AlwaysThrowsServiceImpl();
			var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof (IService), Type.EmptyTypes, MixIn(mixin), interceptor);
			Assert.DoesNotThrow(() => (proxy as IService).Sum(2, 2));
		}
		public void Same_Interface_on_target_of_proxy_with_target_interface_and_mixin_should_forward_to_target()
		{
			var target = new ServiceImpl();
			var mixin = new ServiceImpl();
			IInterceptor interceptor = new WithCallbackInterceptor(i=>
			                                                       	{
			                                                       		Assert.AreSame(target,i.InvocationTarget);
			                                                       		i.ReturnValue = 0;
			                                                       	});
			var proxy = generator.CreateInterfaceProxyWithTargetInterface(typeof(IService), target, MixIn(mixin),interceptor);
			Assert.DoesNotThrow(() => (proxy as IService).Sum(2, 2));
		}
        public void Same_Interface_on_proxy_withouth_target_and_mixin_should_forward_to_null_target()
        {
            var interceptor = new WithCallbackInterceptor(i =>
            {
                Assert.IsNull(i.InvocationTarget);
                i.ReturnValue = 0;
            });
            var mixin = new AlwaysThrowsServiceImpl();
            var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IService), Type.EmptyTypes, MixIn(mixin), interceptor);

            Assert.DoesNotThrow(() => (proxy as IService).Sum(2, 2));
        }
        public void Proxy_without_target_and_last_interceptor_ProceedInfo_succeeds()
        {
            var interceptor = new WithCallbackInterceptor(invocation =>
            {
                invocation.ReturnValue = 0;                          // not relevant to this test, but prevents DP
                                                                     // from complaining about missing return value.
                var proceed = invocation.CaptureProceedInfo();
            });

            var proxy = generator.CreateInterfaceProxyWithoutTarget <IOne>(interceptor);

            proxy.OneMethod();
        }
        public void Test_CreateClassProxy()
        {
            var interceptor = new WithCallbackInterceptor(delegate(IInvocation invocation)
            {
                invocation.Arguments[0] = 5;
                invocation.Arguments[1] = "aaa";
                invocation.Arguments[3] = "bbb";
            });
            var generator = new ProxyGenerator(new PersistentProxyBuilder());
            var proxy     = (MyClass)generator.CreateClassProxy(typeof(MyClass), interceptor);

            Assert.IsNotNull(proxy);
        }
        public void Test_Delegate_SimpleInvocationConstructor()
        {
            var delegateInvocation = new SimpleInvocation(null, null, null);
            var interceptor        = new WithCallbackInterceptor(delegate(IInvocation invocation)
            {
                invocation.Arguments[0] = 5;
                invocation.Arguments[1] = "aaa";
                invocation.Arguments[3] = "bbb";
            });

            interceptor.Intercept(delegateInvocation);
            Assert.AreEqual(delegateInvocation.Arguments[0], 5);
        }
        public void Same_Interface_on_target_of_proxy_with_target_interface_and_mixin_should_forward_to_target()
        {
            var          target      = new ServiceImpl();
            var          mixin       = new ServiceImpl();
            IInterceptor interceptor = new WithCallbackInterceptor(i =>
            {
                Assert.AreSame(target, i.InvocationTarget);
                i.ReturnValue = 0;
            });
            var proxy = generator.CreateInterfaceProxyWithTargetInterface(typeof(IService), target, MixIn(mixin), interceptor);

            Assert.DoesNotThrow(() => (proxy as IService).Sum(2, 2));
        }
        public void Proxy_without_target_and_last_interceptor_ProceedInfo_Invoke_throws_NotImplementedException()
        {
            var interceptor = new WithCallbackInterceptor(invocation =>
            {
                invocation.ReturnValue = 0;                          // not relevant for this test, but prevents DP
                                                                     // from complaining about missing return value.
                var proceed = invocation.CaptureProceedInfo();
                Assert.Throws <NotImplementedException>(() => proceed.Invoke());
            });

            var proxy = generator.CreateInterfaceProxyWithoutTarget <IOne>(interceptor);

            proxy.OneMethod();
        }
        public void CaptureProceedInfo_returns_a_new_object_every_time()
        {
            var interceptor = new WithCallbackInterceptor(invocation =>
            {
                invocation.ReturnValue = 0;                  // not relevant to this test, but prevents DP
                                                             // from complaining about missing return value.
                var proceed1 = invocation.CaptureProceedInfo();
                var proceed2 = invocation.CaptureProceedInfo();
                Assert.AreNotSame(proceed2, proceed1);
            });

            var proxy = generator.CreateInterfaceProxyWithoutTarget <IOne>(interceptor);

            _ = proxy.OneMethod();
        }
        public void Proxy_with_target_and_last_interceptor_ProceedInfo_Invoke_proceeds_to_target()
        {
            var target = new One();

            var interceptor = new WithCallbackInterceptor(invocation =>
            {
                var proceed = invocation.CaptureProceedInfo();
                proceed.Invoke();
            });

            var proxy       = generator.CreateInterfaceProxyWithTarget <IOne>(new One(), interceptor);
            var returnValue = proxy.OneMethod();

            Assert.AreEqual(1, returnValue);
        }
Example #15
0
        public void CanCreateComplexOutRefProxyOnClass()
        {
            var    i  = 3;
            var    s1 = "2";
            string s2;
            var    interceptor = new WithCallbackInterceptor(delegate(IInvocation invocation)
            {
                invocation.Arguments[0] = 5;
                invocation.Arguments[1] = "aaa";
                invocation.Arguments[3] = "bbb";
            });
            var proxy = (MyClass)generator.CreateClassProxy(typeof(MyClass), interceptor);

            proxy.MyMethod(out i, ref s1, 1, out s2);
            Assert.AreEqual(5, i);
            Assert.AreEqual(s1, "aaa");
            Assert.AreEqual(s2, "bbb");
        }