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)); }
public void SetValue_MixedArrayTypes_ObjectArray() { Type type = typeof(FI_FieldArray); object obj = Activator.CreateInstance(type); FieldInfo fieldInfo = GetField(type, "objectArray"); FI_Interface[] mixedMN = new FI_Interface[] { new FI_ClassWithInterface1(), new FI_ClassWithInterface2() }; fieldInfo.SetValue(obj, mixedMN); Assert.Equal(mixedMN, fieldInfo.GetValue(obj)); FI_BaseClass[] BTypeWithAllB_Contra = new FI_SubClass[] { new FI_SubClass(), new FI_SubClass() }; fieldInfo.SetValue(obj, BTypeWithAllB_Contra); Assert.Equal(BTypeWithAllB_Contra, fieldInfo.GetValue(obj)); AssertExtensions.Throws <ArgumentException>(null, () => fieldInfo.SetValue(obj, new int[] { 1, -1, 2, -2 })); AssertExtensions.Throws <ArgumentException>(null, () => fieldInfo.SetValue(obj, new byte[] { 2, 3, 4 })); }
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)); }