Beispiel #1
0
        public static object CreateInstance(Type applicableType, Action <DynamicProxyInvoker> implementationBuildingMethod, params object[] constructorArgs)
        {
            DynamicProxyInvoker invoker = new DynamicProxyInvoker();

            implementationBuildingMethod.Invoke(invoker);
            return(CreateInstance(applicableType, invoker, constructorArgs));
        }
        public void DefaultThrowsException()
        {
            try {
                DynamicProxyInvoker target = new DynamicProxyInvoker();
                IDemoService        proxy1 = DynamicProxy.CreateInstance <IDemoService>(target);
                proxy1.Foo(0, "");

                Assert.Fail("a NotImplementedException should have been thrown, but didnt!");
            }
            catch (NotImplementedException ex) {
                Assert.AreEqual("There is no Implementation for Method Foo(bar, suffix)", ex.Message);
            }
        }
        public void DynamicImplementationOfInterface()
        {
            DynamicProxyInvoker target = new DynamicProxyInvoker();

            target.DefineMethod("Foo",
                                (int bar, string suffix) => {
                return(bar.ToString() + " " + suffix);
            }
                                );

            IDemoService proxy1  = DynamicProxy.CreateInstance <IDemoService>(target);
            string       result1 = proxy1.Foo(123, "Hello");

            Assert.AreEqual("123 Hello", result1);
        }
        public void DefaultWithCustomFallback()
        {
            DynamicProxyInvoker target = new DynamicProxyInvoker();

            target.FallbackInvokeMethod = (method, args, names) => {
                Assert.AreEqual("Foo", method);
                Assert.AreEqual("booooo", args[1]);
                return("hihi");
            };

            IDemoService proxy1 = DynamicProxy.CreateInstance <IDemoService>(target);
            string       result = proxy1.Foo(0, "booooo");

            Assert.AreEqual("hihi", result);
        }
        public void DynamicSpecializationOfClass()
        {
            DynamicProxyInvoker target = new DynamicProxyInvoker();

            target.DefineMethod("GetMyName",
                                () => {
                return("Newton");
            }
                                );

            AnotherServiceToHijack proxy2 = DynamicProxy.CreateInstance <AnotherServiceToHijack>(target);
            string result2 = proxy2.GetMyName();

            Assert.AreEqual("Newton", result2);
        }