コード例 #1
0
ファイル: BasicClassProxyTestCase.cs プロジェクト: belav/Core
        public void ProxyForClass()
        {
            object proxy = generator.CreateClassProxy(
                typeof(ServiceClass),
                new ResultModifierInterceptor()
                );

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

            ServiceClass instance = (ServiceClass)proxy;

            // return value is changed by the interceptor
            Assert.AreEqual(44, instance.Sum(20, 25));

            // return value is changed by the interceptor
            Assert.AreEqual(true, instance.Valid);

            Assert.AreEqual((byte)45, instance.Sum((byte)20, (byte)25));       // byte
            Assert.AreEqual(45L, instance.Sum(20L, 25L));                      // long
            Assert.AreEqual((short)45, instance.Sum((short)20, (short)25));    // short
            Assert.AreEqual(45f, instance.Sum(20f, 25f));                      // float
            Assert.AreEqual(45.0, instance.Sum(20.0, 25.0));                   // double
            Assert.AreEqual((ushort)45, instance.Sum((ushort)20, (ushort)25)); // ushort
            Assert.AreEqual((uint)45, instance.Sum((uint)20, (uint)25));       // uint
            Assert.AreEqual((ulong)45, instance.Sum((ulong)20, (ulong)25));    // ulong
        }
コード例 #2
0
        public void ProxyForClassWithInterfaces()
        {
            object proxy = generator.CreateClassProxy(typeof(ServiceClass), new[] { typeof(IDisposable) },
                                                      new ResultModifierInterceptor());

            Assert.IsNotNull(proxy);
            Assert.IsTrue(typeof(ServiceClass).IsAssignableFrom(proxy.GetType()));
            Assert.IsTrue(typeof(IDisposable).IsAssignableFrom(proxy.GetType()));

            ServiceClass inter = (ServiceClass)proxy;

            Assert.AreEqual(44, inter.Sum(20, 25));
            Assert.AreEqual(true, inter.Valid);

            try
            {
                IDisposable disp = (IDisposable)proxy;
                disp.Dispose();

                Assert.Fail("Expected exception as Dispose has no implementation");
            }
            catch (NotImplementedException ex)
            {
                Assert.AreEqual(
                    "This is a DynamicProxy2 error: The interceptor attempted to 'Proceed' for method 'Void Dispose()' which has no target. " +
                    "When calling method without target there is no implementation to 'proceed' to and it is the responsibility of the interceptor " +
                    "to mimic the implementation (set return value, out arguments etc)",
                    ex.Message);
            }
        }
コード例 #3
0
        public void InvocationForConcreteClassProxy()
        {
            KeepDataInterceptor interceptor = new KeepDataInterceptor();

            object proxy = generator.CreateClassProxy(typeof(ServiceClass), interceptor);

            ServiceClass instance = (ServiceClass)proxy;

            instance.Sum(20, 25);

            Assert.IsNotNull(interceptor.Invocation);

            Assert.IsNotNull(interceptor.Invocation.Arguments);
            Assert.AreEqual(2, interceptor.Invocation.Arguments.Length);
            Assert.AreEqual(20, interceptor.Invocation.Arguments[0]);
            Assert.AreEqual(25, interceptor.Invocation.Arguments[1]);
            Assert.AreEqual(20, interceptor.Invocation.GetArgumentValue(0));
            Assert.AreEqual(25, interceptor.Invocation.GetArgumentValue(1));
            Assert.AreEqual(45, interceptor.Invocation.ReturnValue);

            Assert.IsNotNull(interceptor.Invocation.Proxy);
            Assert.IsInstanceOf(typeof(ServiceClass), interceptor.Invocation.Proxy);

            Assert.IsNotNull(interceptor.Invocation.InvocationTarget);
            Assert.IsInstanceOf(typeof(ServiceClass), interceptor.Invocation.InvocationTarget);
            Assert.IsNotNull(interceptor.Invocation.TargetType);
            Assert.AreSame(typeof(ServiceClass), interceptor.Invocation.TargetType);

            Assert.IsNotNull(interceptor.Invocation.Method);
            Assert.IsNotNull(interceptor.Invocation.MethodInvocationTarget);
            Assert.AreSame(interceptor.Invocation.Method, interceptor.Invocation.MethodInvocationTarget.GetBaseDefinition());
        }
        public void ProxyForClass()
        {
            object proxy = _generator.CreateClassProxy(
                typeof(ServiceClass), new ResultModifiedInvocationHandler( ));

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

            ServiceClass inter = (ServiceClass)proxy;

            Assert.AreEqual(44, inter.Sum(20, 25));
            Assert.AreEqual(true, inter.Valid);
        }
コード例 #5
0
        public void HookIsUsedForConcreteClassProxy()
        {
            LogInvocationInterceptor logger = new LogInvocationInterceptor();
            LogHook hook = new LogHook(typeof(ServiceClass), true);

            ProxyGenerationOptions options = new ProxyGenerationOptions(hook);

            ServiceClass proxy = (ServiceClass)generator.CreateClassProxy(typeof(ServiceClass), options, logger);

            Assert.IsTrue(hook.Completed);
            Assert.AreEqual(10, hook.AskedMembers.Count, "Asked members");

            Assert.AreEqual(2, hook.NonVirtualMembers.Count, "Non-virtual members");            // <-- this would fail due to superfulous method check

            proxy.Sum(1, 2);
            Assert.IsFalse(proxy.Valid);

            Assert.AreEqual("get_Valid ", logger.LogContents);
        }