示例#1
0
        public void SetValue_MixedArrayTypes_CommonBaseClass()
        {
            FI_BaseClass[] ATypeWithMixedAB     = new FI_BaseClass[] { new FI_BaseClass(), new FI_SubClass() };
            FI_BaseClass[] ATypeWithAllA        = new FI_BaseClass[] { new FI_BaseClass(), new FI_BaseClass() };
            FI_BaseClass[] ATypeWithAllB        = new FI_BaseClass[] { new FI_SubClass(), new FI_SubClass() };
            FI_SubClass[]  BTypeWithAllB        = new FI_SubClass[] { new FI_SubClass(), new FI_SubClass() };
            FI_BaseClass[] BTypeWithAllB_Contra = new FI_SubClass[] { new FI_SubClass(), new FI_SubClass() };

            Type      type      = typeof(FI_FieldArray);
            object    obj       = Activator.CreateInstance(type);
            FieldInfo fieldInfo = GetField(type, "aArray");

            fieldInfo.SetValue(obj, ATypeWithMixedAB);
            Assert.Equal(ATypeWithMixedAB, fieldInfo.GetValue(obj));

            fieldInfo.SetValue(obj, ATypeWithAllA);
            Assert.Equal(ATypeWithAllA, fieldInfo.GetValue(obj));

            fieldInfo.SetValue(obj, ATypeWithAllB);
            Assert.Equal(ATypeWithAllB, fieldInfo.GetValue(obj));

            fieldInfo.SetValue(obj, BTypeWithAllB);
            Assert.Equal(BTypeWithAllB, fieldInfo.GetValue(obj));

            fieldInfo.SetValue(obj, BTypeWithAllB_Contra);
            Assert.Equal(BTypeWithAllB_Contra, fieldInfo.GetValue(obj));
        }
示例#2
0
        public void SetValue_MixedArrayTypes_SubClass()
        {
            FI_BaseClass[] ATypeWithMixedAB     = new FI_BaseClass[] { new FI_BaseClass(), new FI_SubClass() };
            FI_BaseClass[] ATypeWithAllA        = new FI_BaseClass[] { new FI_BaseClass(), new FI_BaseClass() };
            FI_BaseClass[] ATypeWithAllB        = new FI_BaseClass[] { new FI_SubClass(), new FI_SubClass() };
            FI_SubClass[]  BTypeWithAllB        = new FI_SubClass[] { new FI_SubClass(), new FI_SubClass() };
            FI_BaseClass[] BTypeWithAllB_Contra = new FI_SubClass[] { new FI_SubClass(), new FI_SubClass() };

            Type      type      = typeof(FI_FieldArray);
            object    obj       = Activator.CreateInstance(type);
            FieldInfo fieldInfo = GetField(type, "bArray");

            AssertExtensions.Throws <ArgumentException>(null, () => fieldInfo.SetValue(obj, ATypeWithMixedAB));
            AssertExtensions.Throws <ArgumentException>(null, () => fieldInfo.SetValue(obj, ATypeWithAllA));
            AssertExtensions.Throws <ArgumentException>(null, () => fieldInfo.SetValue(obj, ATypeWithAllB));

            fieldInfo.SetValue(obj, BTypeWithAllB);
            Assert.Equal(BTypeWithAllB, fieldInfo.GetValue(obj));

            fieldInfo.SetValue(obj, BTypeWithAllB_Contra);
            Assert.Equal(BTypeWithAllB_Contra, fieldInfo.GetValue(obj));
        }