Exemplo n.º 1
0
    public static void TestDynamicInvokeCastingByRef()
    {
        {
            Delegate d    = new DFoo2(Foo2);
            Object[] args = { 7 };
            d.DynamicInvoke(args);
            Assert.Equal(args[0], 8);
        }

        {
            Delegate d    = new DFoo2(Foo2);
            Object[] args = { null };
            d.DynamicInvoke(args);
            Assert.Equal(args[0], 1);
        }

        // for "byref ValueType" arguments, the incoming is allowed to be null. The target will receive default(ValueType).
        {
            Delegate d    = new DFoo3(Foo3);
            Object[] args = { null };
            d.DynamicInvoke(args);
            MyStruct s = (MyStruct)(args[0]);
            Assert.Equal(s.X, 7);
            Assert.Equal(s.Y, 8);
        }

        // For "byref ValueType" arguments, the type must match exactly.
        {
            Delegate d    = new DFoo2(Foo2);
            Object[] args = { (uint)7 };
            Assert.Throws <ArgumentException>(() => d.DynamicInvoke(args));
        }

        {
            Delegate d    = new DFoo2(Foo2);
            Object[] args = { E4.One };
            Assert.Throws <ArgumentException>(() => d.DynamicInvoke(args));
        }

        return;
    }
Exemplo n.º 2
0
        public static void DynamicInvoke_CastingByRef()
        {
            {
                Delegate d = new DFoo2(Foo2);
                object[] args = { 7 };
                d.DynamicInvoke(args);
                Assert.Equal(args[0], 8);
            }

            {
                Delegate d = new DFoo2(Foo2);
                object[] args = { null };
                d.DynamicInvoke(args);
                Assert.Equal(args[0], 1);
            }

            // for "byref ValueType" arguments, the incoming is allowed to be null. The target will receive default(ValueType).
            {
                Delegate d = new DFoo3(Foo3);
                object[] args = { null };
                d.DynamicInvoke(args);
                MyStruct s = (MyStruct)(args[0]);
                Assert.Equal(s.X, 7);
                Assert.Equal(s.Y, 8);
            }

            // For "byref ValueType" arguments, the type must match exactly.
            {
                Delegate d = new DFoo2(Foo2);
                object[] args = { (uint)7 };
                Assert.Throws<ArgumentException>(() => d.DynamicInvoke(args));
            }

            {
                Delegate d = new DFoo2(Foo2);
                object[] args = { E4.One };
                Assert.Throws<ArgumentException>(() => d.DynamicInvoke(args));
            }
        }