예제 #1
0
        public void AddForwardingProperty()
        {
            var proxyBuilder = new ForwardingProxyBuilder("AddForwardingProperty",
                                                          ModuleScope, typeof(ProxiedChildGeneric <ProxiedChild, double>), new Type[0]);
            var propertyInfo    = typeof(ProxiedChildGeneric <ProxiedChild, double>).GetProperty("MutableName");
            var propertyEmitter = proxyBuilder.AddForwardingProperty(propertyInfo);

            // Added by FS
            Assert.That(propertyEmitter.Name, Is.EqualTo(propertyInfo.Name));
            Assert.That(propertyEmitter.PropertyType, Is.SameAs(propertyInfo.PropertyType));

            // 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);
            var proxyType = proxyBuilder.BuildProxyType();
            //object proxy = proxyBuilder.CreateInstance (proxied);
            object proxy = Activator.CreateInstance(proxyType, proxied);

            Assert.That(proxied.MutableName, Is.EqualTo("ProxiedChild: PCG"));

            //var proxyPropertyInfo = proxy.GetType ().GetProperty ("MutableName");
            var proxyPropertyInfo = proxyType.GetProperty("MutableName");

            AssertPropertyInfoEqual(proxyPropertyInfo, propertyInfo);
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo("ProxiedChild: PCG"));

            proxied.MutableName = "PCG_Changed";
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo("ProxiedChild: PCG_Changed"));

            proxyPropertyInfo.SetValue(proxy, "PCG_Changed_Proxy", null);
            Assert.That(proxied.MutableName, Is.EqualTo("ProxiedChild: PCG_Changed_Proxy"));
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo("ProxiedChild: PCG_Changed_Proxy"));
        }
예제 #2
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"));
        }
예제 #3
0
        public void BuildProxyType()
        {
            var  proxyBuilder = new ForwardingProxyBuilder("ForwardingProxyBuilder_BuildProxyTypeTest", ModuleScope, typeof(Proxied), new Type[0]);
            Type proxyType    = proxyBuilder.BuildProxyType();

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

            FieldInfo proxiedFieldInfo = proxy.GetType().GetField("_proxied", _nonPublicInstanceFlags);

            Assert.That(proxiedFieldInfo.GetValue(proxy), Is.EqualTo(proxied));
            Assert.That(proxiedFieldInfo.IsInitOnly, Is.False);
            Assert.That(proxiedFieldInfo.IsPrivate, Is.True);
        }
예제 #4
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();
            }
        }
예제 #5
0
        public void AddForwardingExplicitInterfaceMethod()
        {
            var proxyBuilder = new ForwardingProxyBuilder("AddForwardingExplicitInterfaceMethod",
                                                          ModuleScope, typeof(ProxiedChild), new[] { typeof(IAmbigous1) });

            var methodInfo = typeof(IAmbigous1).GetMethod("StringTimes");

            proxyBuilder.AddForwardingExplicitInterfaceMethod(methodInfo);

            // Create proxy instance, initializing it with class to be proxied
            var    proxied = new ProxiedChild();
            object proxy   = proxyBuilder.CreateInstance(proxied);

            Assert.That(((IAmbigous1)proxied).StringTimes("aBc", 4), Is.EqualTo("aBcaBcaBcaBc"));
            Assert.That(((IAmbigous1)proxy).StringTimes("aBc", 4), Is.EqualTo("aBcaBcaBcaBc"));
        }
예제 #6
0
        public void IProxy()
        {
            var  proxyBuilder = new ForwardingProxyBuilder("IProxy", ModuleScope, typeof(Proxied), new Type[0]);
            Type proxyType    = proxyBuilder.BuildProxyType();

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

            Assert.That(ScriptingHelper.GetProxiedFieldValue(proxy), Is.SameAs(proxied));

            var proxied2 = new Proxied("BBB");

            ((IProxy)proxy).SetProxied(proxied2);

            Assert.That(ScriptingHelper.GetProxiedFieldValue(proxy), Is.SameAs(proxied2));
        }
예제 #7
0
        public void AddForwardingMethodFromMethodInfoCopy_ImplicitInterfaceMethod()
        {
            var proxyBuilder = new ForwardingProxyBuilder("AddForwardingMethodFromMethodInfoCopy_ImplicitInterfaceMethod",
                                                          ModuleScope, typeof(Proxied), new[] { typeof(IProxiedGetName) });

            proxyBuilder.AddForwardingMethodFromClassOrInterfaceMethodInfoCopy(typeof(IProxiedGetName).GetMethod("GetName"));
            Type proxyType = proxyBuilder.BuildProxyType();

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

            Assert.That(proxy.GetType().GetInterfaces(), Is.EquivalentTo((new[] { typeof(IProxiedGetName), typeof(IProxy) })));
            Assert.That(((IProxiedGetName)proxy).GetName(), Is.EqualTo("Implementer.IProxiedGetName"));
            Assert.That(proxy.GetType().GetMethod("GetName").Invoke(proxy, new object[0]), Is.EqualTo("Implementer.IProxiedGetName"));
        }
예제 #8
0
        public void AddForwardingProperty_WriteOnlyProperty()
        {
            Type proxiedType  = typeof(Proxied);
            var  proxyBuilder = new ForwardingProxyBuilder("AddForwardingProperty_WriteOnlyProperty", ModuleScope, proxiedType, new Type[0]);

            var propertyInfo    = proxiedType.GetProperty("WriteonlyName");
            var propertyEmitter = proxyBuilder.AddForwardingProperty(propertyInfo);

            // Added by FS
            Assert.That(propertyEmitter.Name, Is.EqualTo(propertyInfo.Name));
            Assert.That(propertyEmitter.PropertyType, Is.SameAs(propertyInfo.PropertyType));

            Type proxyType         = proxyBuilder.BuildProxyType();
            var  proxyPropertyInfo = proxyType.GetProperty("WriteonlyName");

            Assert.That(proxyPropertyInfo.CanRead, Is.False);
            Assert.That(proxyPropertyInfo.CanWrite, Is.True);
        }
예제 #9
0
        public void AddForwardingMethodFromMethodInfoCopy_Method()
        {
            var proxyBuilder = new ForwardingProxyBuilder("AddForwardingMethodFromMethodInfoCopy_Method", ModuleScope, typeof(Proxied), new Type[0]);
            var methodInfo   = typeof(Proxied).GetMethod("PrependName");

            proxyBuilder.AddForwardingMethodFromClassOrInterfaceMethodInfoCopy(methodInfo);
            Type proxyType = proxyBuilder.BuildProxyType();

            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"));
        }
예제 #10
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"));
        }