Beispiel #1
0
        public void AddForwardingMethod_MethodWithVariableNumberOfParameters()
        {
            var proxyBuilder = new ForwardingProxyBuilder("AddForwardingProperty_MethodWithVariableNumberOfParameters", ModuleScope, typeof(Proxied), new Type[0]);
            var methodInfo   = typeof(Proxied).GetMethod("Sum");

            var methodEmitter = proxyBuilder.AddForwardingMethod(methodInfo);

            Type proxyType = proxyBuilder.BuildProxyType();

            // Added by FS
            AssertMethodInfoEqual(methodEmitter.MethodBuilder, methodInfo);

            // Create proxy instance, initializing it with class to be proxied
            var    proxied = new Proxied("ProxiedProxySumTest");
            object proxy   = Activator.CreateInstance(proxyType, proxied);

            // Check calling proxied method through reflection
            const string resultExpected = "ProxiedProxySumTest: 12";

            Assert.That(proxied.Sum(3, 4, 5), Is.EqualTo(resultExpected));
            Assert.That(methodInfo.Invoke(proxied, new object[] { new int[] { 3, 4, 5 } }), Is.EqualTo(resultExpected));

            var proxyMethodInfo = proxy.GetType().GetMethod("Sum");

            AssertMethodInfoEqual(proxyMethodInfo, methodInfo);
            Assert.That(proxyMethodInfo.Invoke(proxy, new object[] { new int[] { 3, 4, 5 } }), Is.EqualTo(resultExpected));

            Assert.That(proxyMethodInfo.Invoke(proxy, new object[] { new int[] { } }), Is.EqualTo("ProxiedProxySumTest: 0"));
            Assert.That(proxyMethodInfo.Invoke(proxy, new object[] { new int[] { 1 } }), Is.EqualTo("ProxiedProxySumTest: 1"));
            Assert.That(proxyMethodInfo.Invoke(proxy, new object[] { new int[] { 1000, 100, 10, 1 } }), Is.EqualTo("ProxiedProxySumTest: 1111"));
        }
Beispiel #2
0
        public void AddForwardingMethod_NonPublicMethod()
        {
            var proxyBuilder = new ForwardingProxyBuilder("AddForwardingMethod_NonPublicMethod", ModuleScope, typeof(ProxiedChild), new Type[0]);
            var methodInfo   = typeof(ProxiedChild).GetMethod(
                "Remotion.Scripting.UnitTests.TestDomain.IAmbigous1.StringTimes", _nonPublicInstanceFlags);

            try
            {
                proxyBuilder.AddForwardingMethod(methodInfo);
            }
            finally
            {
                proxyBuilder.BuildProxyType();
            }
        }
Beispiel #3
0
        public void AddForwardingMethod_GenericClass()
        {
            var proxyBuilder = new ForwardingProxyBuilder("AddForwardingMethod_GenericClass",
                                                          ModuleScope, typeof(ProxiedChildGeneric <ProxiedChild, double>), new Type[0]);
            var methodInfo    = typeof(ProxiedChildGeneric <ProxiedChild, double>).GetMethod("ToStringKebap");
            var methodEmitter = proxyBuilder.AddForwardingMethod(methodInfo);

            // Create proxy instance, initializing it with class to be proxied
            var    proxied = new ProxiedChildGeneric <ProxiedChild, double> ("PCG", new ProxiedChild("PC"), 123.456);
            object proxy   = proxyBuilder.CreateInstance(proxied);

            // Added by FS
            AssertMethodInfoEqual(methodEmitter.MethodBuilder, methodInfo);

            var proxyMethodInfo = proxy.GetType().GetMethod("ToStringKebap");

            AssertMethodInfoEqual(proxyMethodInfo, methodInfo);
            Assert.That(proxyMethodInfo.Invoke(proxy, new object[] { 9800 }), Is.EqualTo("ProxiedChild: PCG_[Proxied: PC]_123.456_9800"));
        }
Beispiel #4
0
        public void AddForwardingMethod()
        {
            var proxyBuilder  = new ForwardingProxyBuilder("AddForwardingMethod", ModuleScope, typeof(Proxied), new Type[0]);
            var methodInfo    = typeof(Proxied).GetMethod("PrependName");
            var methodEmitter = proxyBuilder.AddForwardingMethod(methodInfo);

            Type proxyType = proxyBuilder.BuildProxyType();

            // Added by FS
            AssertMethodInfoEqual(methodEmitter.MethodBuilder, methodInfo);

            var    proxied = new Proxied("The name");
            object proxy   = Activator.CreateInstance(proxyType, proxied);

            // Check calling proxied method through reflection
            Assert.That(methodInfo.Invoke(proxied, new object[] { "is Smith" }), Is.EqualTo("The name is Smith"));

            var proxyMethodInfo = proxy.GetType().GetMethod("PrependName");

            AssertMethodInfoEqual(proxyMethodInfo, methodInfo);
            Assert.That(proxyMethodInfo.Invoke(proxy, new object[] { "is Smith" }), Is.EqualTo("The name is Smith"));
        }