public void ShouldResolveAmbiguityByDiscardingGenericMethods()
        {
            var service    = new GenericInterfaceImpl(new[] { "a", "b", "c" });
            var dispatcher = DispatcherFactory.CreateDispatcher(service, null, false);

            dispatcher.Execute("GetItems", new Dictionary <string, string>()).Result.ShouldBe("[a,b,c]");
        }
示例#2
0
        public void DelegateConverterTest()
        {
            var dConv = new DelegateConverter();

            Assert.IsFalse(dConv.CanConvert(typeof(Func <object>), typeof(ICloneable)));

            Assert.IsTrue(dConv.CanConvert(typeof(EventHandler), typeof(Action <object, EventArgs>)));

            Func <object, bool> t1 = (b) => { return(b != null); };

            var customDelegate1 = (CustomDelegateType)dConv.Convert(t1, typeof(CustomDelegateType));

            Assert.IsTrue(customDelegate1("somestr"));

            Func <object, object> t2 = (b) => { return(b != null); };
            var customDelegate2      = (CustomDelegateType)dConv.Convert(t2, typeof(CustomDelegateType));

            Assert.IsTrue(customDelegate1("somestr"));

            /*var stopWatch1 = new System.Diagnostics.Stopwatch();
             * stopWatch1.Start();
             * for (int i = 0; i < 100000; i++) {
             *      customDelegate1("somestr");
             * }
             * stopWatch1.Stop();
             * Console.WriteLine("Deleg -> Deleg: {0}", stopWatch1.Elapsed.ToString());*/


            Func <Hashtable> t3 = () => { return(new Hashtable()
                {
                    { "a", 1 }
                }); };
            var t3gen           = (Func <IDictionary <string, object> >)dConv.Convert(t3, typeof(Func <IDictionary <string, object> >));

            Assert.AreEqual(1, t3gen()["a"]);

            // test explicit converter to interface
            string doSomethingRes = null;
            // from delegate
            Action <object, int> doSomething = (o, i) => {
                doSomethingRes = String.Format("{0}_{1}", o, i);
            };

            Assert.True(dConv.CanConvert(doSomething.GetType(), typeof(InterfaceWithConv)));

            var doSomethingInterface = dConv.Convert(doSomething, typeof(InterfaceWithConv)) as InterfaceWithConv;

            doSomethingInterface.DoSomething("a", 1);
            Assert.AreEqual("a_1", doSomethingRes);
            Assert.IsTrue(doSomethingInterface is InterfaceConv.Proxy);

            /*var stopWatch2 = new System.Diagnostics.Stopwatch();
             * stopWatch2.Start();
             * for (int i = 0; i < 100000; i++) {
             *      doSomethingInterface.DoSomething("a", 1);
             * }
             * stopWatch2.Stop();
             * Console.WriteLine("Deleg -> SAM (Custom TypeConverter proxy): {0}", stopWatch2.Elapsed.ToString());*/


            // from another interface
            var doSomethingImpl = new CompatibleInterfaceImpl();

            Assert.True(dConv.CanConvert(doSomethingImpl.GetType(), typeof(InterfaceWithConv)));

            doSomethingInterface = dConv.Convert(doSomethingImpl, typeof(InterfaceWithConv)) as InterfaceWithConv;
            doSomethingInterface.DoSomething("b", 2);
            Assert.AreEqual("b_2", doSomethingImpl.Res);


            // from delegate to interface using realproxy
            Func <object, int, int> doSomething2 = (o, i) => {
                return(String.Format("{0}_{1}", o, i).Length);
            };

            Assert.True(dConv.CanConvert(doSomething2.GetType(), typeof(CompatibleInterface)));

            var doSomethingInterface2 = dConv.Convert(doSomething2, typeof(CompatibleInterface)) as CompatibleInterface;

            Assert.AreEqual(5, doSomethingInterface2.Update("zz", "10"));

            // from SAM to SAM using realproxy
            var doSomethingInterface3 = dConv.Convert(doSomethingImpl, typeof(CompatibleInterface2)) as CompatibleInterface2;

            Assert.AreEqual(6, doSomethingInterface3.GetLen(1, "test"));

            // extra check - for result contravariance
            Func <string, object, string> updateStr = (s, o) => { return(s.Length.ToString()); };
            var doSomethingInterface4 = dConv.Convert(updateStr, typeof(CompatibleInterface)) as CompatibleInterface;

            Assert.AreEqual(3, doSomethingInterface4.Update("abc", null));

            /*var stopWatch3 = new System.Diagnostics.Stopwatch();
             * stopWatch3.Start();
             * for (int i = 0; i < 100000; i++) {
             *      doSomethingInterface3.GetLen(1, "test");
             * }
             * stopWatch3.Stop();
             * Console.WriteLine("SAM -> SAM (RealProxy): {0}", stopWatch3.Elapsed.ToString());*/

            // test with generic interfaces
            var constPrv = new GenericInterfaceImpl();

            Assert.True(dConv.CanConvert(constPrv.GetType(), typeof(Func <int, int>)));
            var getConstDeleg = (Func <int, int>)dConv.Convert(constPrv, typeof(Func <int, int>));

            Assert.AreEqual("5", getConstDeleg(5).ToString());

            Action <int> a1 = (a) => { };

            Assert.True(dConv.CanConvert(typeof(Action <int>), typeof(Action <object>)));
            var castAction = dConv.Convert(a1, typeof(Action <object>));
        }