示例#1
0
    public static bool TestInterfaceDelegates()
    {
        Console.Write("Testing delegates to interface methods...");

        {
            IFoo t = new ClassWithIFoo("Class");
            Func <int, string> d = t.DoFoo;
            if (d(987) != "Class987")
            {
                return(false);
            }
        }

        {
            IFoo t = new StructWithIFoo("Struct");
            Func <int, string> d = t.DoFoo;
            if (d(654) != "Struct654")
            {
                return(false);
            }
        }

        Console.WriteLine("OK");
        return(true);
    }
示例#2
0
    public static bool TestVirtualDelegates()
    {
        Console.Write("Testing delegates to virtual methods...");

        {
            Mid t = new Mid();
            if (t.GetBaseDo()() != "Base")
            {
                return(false);
            }

            if (t.GetDerivedDo()() != "Mid")
            {
                return(false);
            }
        }

        {
            Mid t = new Derived();
            if (t.GetBaseDo()() != "Base")
            {
                return(false);
            }

            if (t.GetDerivedDo()() != "Derived")
            {
                return(false);
            }
        }

        {
            // This will end up being a delegate to a sealed virtual method.
            ClassWithIFoo      t = new ClassWithIFoo("Class");
            Func <int, string> d = t.DoFoo;
            if (d(987) != "Class987")
            {
                return(false);
            }
        }

        Console.WriteLine("OK");
        return(true);
    }
示例#3
0
    public static bool TestDynamicInvoke()
    {
        Console.Write("Testing dynamic invoke...");

        {
            TestValueType t = new TestValueType {
                X = 123
            };
            Func <string, string> d = t.GiveX;
            string result           = (string)d.DynamicInvoke(new object[] { "MyPrefix" });
            if (result != "MyPrefix123")
            {
                return(false);
            }
        }

        {
            Func <int, TestValueType> d      = TestValueType.MakeValueType;
            TestValueType             result = (TestValueType)d.DynamicInvoke(new object[] { 789 });
            if (result.X != 789)
            {
                return(false);
            }
        }

        {
            IFoo t = new ClassWithIFoo("Class");
            Func <int, string> d = t.DoFoo;
            if ((string)d.DynamicInvoke(new object[] { 987 }) != "Class987")
            {
                return(false);
            }
        }

        {
            IFoo t = new StructWithIFoo("Struct");
            Func <int, string> d = t.DoFoo;
            if ((string)d.DynamicInvoke(new object[] { 654 }) != "Struct654")
            {
                return(false);
            }
        }

        {
            Func <string, string, string> d = ExtensionClass.Combine;
            if ((string)d.DynamicInvoke(new object[] { "Hello", "World" }) != "HelloWorld")
            {
                return(false);
            }
        }

        {
            Func <string, string> d = "Hi".Combine;
            if ((string)d.DynamicInvoke(new object[] { "There" }) != "HiThere")
            {
                return(false);
            }
        }

        {
            Mutate <int> d    = ClassWithByRefs.Mutate;
            object[]     args = new object[] { 8 };
            d.DynamicInvoke(args);
            if ((int)args[0] != 50)
            {
                return(false);
            }
        }

        {
            Mutate <string> d    = ClassWithByRefs.Mutate;
            object[]        args = new object[] { "Hello" };
            d.DynamicInvoke(args);
            if ((string)args[0] != "HelloMutated")
            {
                return(false);
            }
        }

        unsafe
        {
            GetAndReturnPointerDelegate d = ClassWithPointers.GetAndReturnPointer;
            if ((IntPtr)d.DynamicInvoke(new object[] { (IntPtr)8 }) != (IntPtr)50)
            {
                return(false);
            }
        }

#if false
        // This is hitting an EH bug around throw/rethrow from a catch block (pass is not set properly)
        unsafe
        {
            PassPointerByRefDelegate d = ClassWithPointers.PassPointerByRef;
            var args = new object[] { (IntPtr)8 };

            bool caught = false;
            try
            {
                d.DynamicInvoke(args);
            }
            catch (ArgumentException)
            {
                caught = true;
            }

            if (!caught)
            {
                return(false);
            }
        }
#endif

        Console.WriteLine("OK");
        return(true);
    }
示例#4
0
    public static bool TestInterfaceDelegates()
    {
        Console.Write("Testing delegates to interface methods...");

        {
            IFoo t = new ClassWithIFoo("Class");
            Func<int, string> d = t.DoFoo;
            if (d(987) != "Class987")
                return false;
        }

        {
            IFoo t = new StructWithIFoo("Struct");
            Func<int, string> d = t.DoFoo;
            if (d(654) != "Struct654")
                return false;
        }

        Console.WriteLine("OK");
        return true;
    }
示例#5
0
    public static bool TestDynamicInvoke()
    {
        Console.Write("Testing dynamic invoke...");

        {
            TestValueType t = new TestValueType { X = 123 };
            Func<string, string> d = t.GiveX;
            string result = (string)d.DynamicInvoke(new object[] { "MyPrefix" });
            if (result != "MyPrefix123")
                return false;
        }

        {
            Func<int, TestValueType> d = TestValueType.MakeValueType;
            TestValueType result = (TestValueType)d.DynamicInvoke(new object[] { 789 });
            if (result.X != 789)
                return false;
        }

        {
            IFoo t = new ClassWithIFoo("Class");
            Func<int, string> d = t.DoFoo;
            if ((string)d.DynamicInvoke(new object[] { 987 }) != "Class987")
                return false;
        }

        {
            IFoo t = new StructWithIFoo("Struct");
            Func<int, string> d = t.DoFoo;
            if ((string)d.DynamicInvoke(new object[] { 654 }) != "Struct654")
                return false;
        }

        {
            Func<string, string, string> d = ExtensionClass.Combine;
            if ((string)d.DynamicInvoke(new object[] { "Hello", "World" }) != "HelloWorld")
                return false;
        }

        {
            Func<string, string> d = "Hi".Combine;
            if ((string)d.DynamicInvoke(new object[] { "There" }) != "HiThere")
                return false;
        }

        {
            Mutate<int> d = ClassWithByRefs.Mutate;
            object[] args = new object[] { 8 };
            d.DynamicInvoke(args);
            if ((int)args[0] != 50)
                return false;
        }

        {
            Mutate<string> d = ClassWithByRefs.Mutate;
            object[] args = new object[] { "Hello" };
            d.DynamicInvoke(args);
            if ((string)args[0] != "HelloMutated")
                return false;
        }

        unsafe
        {
            GetAndReturnPointerDelegate d = ClassWithPointers.GetAndReturnPointer;
            if ((IntPtr)d.DynamicInvoke(new object[] { (IntPtr)8 }) != (IntPtr)50)
                return false;
        }

        #if false
        // This is hitting an EH bug around throw/rethrow from a catch block (pass is not set properly)
        unsafe
        {
            PassPointerByRefDelegate d = ClassWithPointers.PassPointerByRef;
            var args = new object[] { (IntPtr)8 };

            bool caught = false;
            try
            {
                d.DynamicInvoke(args);
            }
            catch (ArgumentException)
            {
                caught = true;
            }

            if (!caught)
                return false;
        }
        #endif

        Console.WriteLine("OK");
        return true;
    }