public static void TestSetValue_Field1()
        {
            FieldInfo fi   = null;
            Type      type = typeof(ArrayAsField);
            Object    obj  = null;


            obj = Activator.CreateInstance(type);
            A[] ATypeWithMixedAB            = new A[] { new A(), new FieldInfoArrayB() };
            A[] ATypeWithAllA               = new A[] { new A(), new A() };
            A[] ATypeWithAllB               = new A[] { new FieldInfoArrayB(), new FieldInfoArrayB() };
            FieldInfoArrayB[] BTypeWithAllB = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };
            A[] BTypeWithAllB_Contra        = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };

            fi = getField(type, "aArray");

            fi.SetValue(obj, ATypeWithMixedAB);
            Assert.True((fi.GetValue(obj)).Equals(ATypeWithMixedAB), "Failed!! Could not set ArrayField aArray using FieldInfo");

            fi.SetValue(obj, ATypeWithAllA);
            Assert.True((fi.GetValue(obj)).Equals(ATypeWithAllA), "Failed!! Could not set ArrayField aArray using FieldInfo");

            fi.SetValue(obj, ATypeWithAllB);
            Assert.True((fi.GetValue(obj)).Equals(ATypeWithAllB), "Failed!! Could not set ArrayField aArray using FieldInfo");

            fi.SetValue(obj, BTypeWithAllB);
            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB), "Failed!! Could not set ArrayField aArray using FieldInfo");

            fi.SetValue(obj, BTypeWithAllB_Contra);
            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB_Contra), "Failed!! Could not set ArrayField aArray using FieldInfo");
        }
        public static void TestSetValue_Field5()
        {
            FieldInfo fi   = null;
            Type      type = typeof(ArrayAsField);
            Object    obj  = Activator.CreateInstance(type);

            I[]    mixedMN              = new I[] { new M(), new N() };
            int[]  mixedInt             = new int[] { 200, -200, (byte)30, (ushort)2 };
            byte[] allByte              = new byte[] { 2, 3, 4 };
            A[]    BTypeWithAllB_Contra = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };


            fi = getField(type, "objectArray");
            fi.SetValue(obj, mixedMN);
            Assert.True((fi.GetValue(obj)).Equals(mixedMN), "Failed!! Could not set ArrayField objectArray using FieldInfo");

            fi.SetValue(obj, BTypeWithAllB_Contra);

            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB_Contra), "Failed!! Could not set ArrayField objectArray using FieldInfo");

            try
            {
                fi.SetValue(obj, mixedInt);
                Assert.False(true, "Failed!! Expected ArgumentException.System.Int32[]' cannot be converted to type 'System.Object[]'");
            }
            catch (Exception) { }

            try
            {
                fi.SetValue(obj, allByte);
                Assert.False(true, "Failed!! Expected ArgumentException.System.Byte[]' cannot be converted to type 'System.Object[]");
            }
            catch (Exception) { }
        }
示例#3
0
        public static void TestSetValue_Field2()
        {
            FieldInfo fi = null;
            Type type = typeof(ArrayAsField);
            Object obj = null;


            obj = Activator.CreateInstance(type);
            A[] ATypeWithMixedAB = new A[] { new A(), new FieldInfoArrayB() };
            A[] ATypeWithAllA = new A[] { new A(), new A() };
            A[] ATypeWithAllB = new A[] { new FieldInfoArrayB(), new FieldInfoArrayB() };
            FieldInfoArrayB[] BTypeWithAllB = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };
            A[] BTypeWithAllB_Contra = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };

            fi = getField(type, "bArray");
            Assert.Throws<ArgumentException>(() => fi.SetValue(obj, ATypeWithMixedAB));
            Assert.Throws<ArgumentException>(() => fi.SetValue(obj, ATypeWithAllA));

            Assert.Throws<ArgumentException>(() => fi.SetValue(obj, ATypeWithAllB));

            fi.SetValue(obj, BTypeWithAllB);
            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB), "Failed!! Could not set ArrayField bArray using FieldInfo");

            fi.SetValue(obj, BTypeWithAllB_Contra);
            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB_Contra), "Failed!! Could not set ArrayField bArray using FieldInfo");
        }
        public static void TestSetValue_Field2()
        {
            FieldInfo fi   = null;
            Type      type = typeof(ArrayAsField);
            Object    obj  = null;


            obj = Activator.CreateInstance(type);
            A[] ATypeWithMixedAB            = new A[] { new A(), new FieldInfoArrayB() };
            A[] ATypeWithAllA               = new A[] { new A(), new A() };
            A[] ATypeWithAllB               = new A[] { new FieldInfoArrayB(), new FieldInfoArrayB() };
            FieldInfoArrayB[] BTypeWithAllB = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };
            A[] BTypeWithAllB_Contra        = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };

            fi = getField(type, "bArray");

            try
            {
                fi.SetValue(obj, ATypeWithMixedAB);
                Assert.False(true, "Failed!! Expected ArgumentException.");
            }
            catch (Exception) { }

            try
            {
                fi.SetValue(obj, ATypeWithAllA);
                Assert.False(true, "Failed!! Expected ArgumentException.");
            }
            catch (Exception) { }

            try
            {
                fi.SetValue(obj, ATypeWithAllB);
                Assert.False(true, "Failed!! Expected ArgumentException.");
            }
            catch (Exception) { }



            fi.SetValue(obj, BTypeWithAllB);
            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB), "Failed!! Could not set ArrayField bArray using FieldInfo");

            fi.SetValue(obj, BTypeWithAllB_Contra);
            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB_Contra), "Failed!! Could not set ArrayField bArray using FieldInfo");
        }
示例#5
0
        public static void TestSetValue_Field5()
        {
            FieldInfo fi   = null;
            Type      type = typeof(ArrayAsField);
            Object    obj  = Activator.CreateInstance(type);

            I[]    mixedMN              = new I[] { new M(), new N() };
            int[]  mixedInt             = new int[] { 200, -200, (byte)30, (ushort)2 };
            byte[] allByte              = new byte[] { 2, 3, 4 };
            A[]    BTypeWithAllB_Contra = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };


            fi = getField(type, "objectArray");
            fi.SetValue(obj, mixedMN);
            Assert.True((fi.GetValue(obj)).Equals(mixedMN), "Failed!! Could not set ArrayField objectArray using FieldInfo");

            fi.SetValue(obj, BTypeWithAllB_Contra);

            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB_Contra), "Failed!! Could not set ArrayField objectArray using FieldInfo");
            Assert.Throws <ArgumentException>(() => fi.SetValue(obj, mixedInt));
            Assert.Throws <ArgumentException>(() => fi.SetValue(obj, allByte));
        }
示例#6
0
        public static void TestSetValue_Field5()
        {
            FieldInfo fi = null;
            Type type = typeof(ArrayAsField);
            Object obj = Activator.CreateInstance(type);

            I[] mixedMN = new I[] { new M(), new N() };
            int[] mixedInt = new int[] { 200, -200, (byte)30, (ushort)2 };
            byte[] allByte = new byte[] { 2, 3, 4 };
            A[] BTypeWithAllB_Contra = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };


            fi = getField(type, "objectArray");
            fi.SetValue(obj, mixedMN);
            Assert.True((fi.GetValue(obj)).Equals(mixedMN), "Failed!! Could not set ArrayField objectArray using FieldInfo");

            fi.SetValue(obj, BTypeWithAllB_Contra);

            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB_Contra), "Failed!! Could not set ArrayField objectArray using FieldInfo");
            Assert.Throws<ArgumentException>(() => fi.SetValue(obj, mixedInt));
            Assert.Throws<ArgumentException>(() => fi.SetValue(obj, allByte));
        }
示例#7
0
        public static void TestSetValue_Field5()
        {
            FieldInfo fi = null;
            Type type = typeof(ArrayAsField);
            Object obj = Activator.CreateInstance(type);

            I[] mixedMN = new I[] { new M(), new N() };
            int[] mixedInt = new int[] { 200, -200, (byte)30, (ushort)2 };
            byte[] allByte = new byte[] { 2, 3, 4 };
            A[] BTypeWithAllB_Contra = new FieldInfoArrayB[] { new FieldInfoArrayB(), new FieldInfoArrayB() };


            fi = getField(type, "objectArray");
            fi.SetValue(obj, mixedMN);
            Assert.True((fi.GetValue(obj)).Equals(mixedMN), "Failed!! Could not set ArrayField objectArray using FieldInfo");

            fi.SetValue(obj, BTypeWithAllB_Contra);

            Assert.True((fi.GetValue(obj)).Equals(BTypeWithAllB_Contra), "Failed!! Could not set ArrayField objectArray using FieldInfo");

            try
            {
                fi.SetValue(obj, mixedInt);
                Assert.False(true, "Failed!! Expected ArgumentException.System.Int32[]' cannot be converted to type 'System.Object[]'");
            }
            catch (Exception) { }

            try
            {
                fi.SetValue(obj, allByte);
                Assert.False(true, "Failed!! Expected ArgumentException.System.Byte[]' cannot be converted to type 'System.Object[]");
            }
            catch (Exception) { }
        }