public void FieldsBasic() { var fieldsType = this._assembly.GetType("AssemblyToProcess.Basic.Fields"); var fieldsInstance = (dynamic)Activator.CreateInstance(fieldsType); PropertyExtensions.SetPrivateFieldValue(fieldsInstance, "_privateField", "I'm private."); fieldsType.GetField("PublicField").SetValue(fieldsInstance, 123); // Hand copy. var hCopy = fieldsInstance.HCopy(); var hGetter = new ObjectGetter(fieldsType, hCopy); Assert.Equal("I'm private.", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(123, hGetter.FieldValue("PublicField")); // Deep copy. var dCopy = fieldsInstance.DeepCopy(); var dGetter = new ObjectGetter(fieldsType, dCopy); Assert.Equal("I'm private.", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(123, dGetter.FieldValue("PublicField")); }
public void FieldsBasic() { var fieldsType = this._assembly.GetType("AssemblyToProcess.Basic.Fields"); var fieldsInstance = (dynamic) Activator.CreateInstance(fieldsType); PropertyExtensions.SetPrivateFieldValue(fieldsInstance, "_privateField", "I'm private."); fieldsType.GetField("PublicField").SetValue(fieldsInstance, 123); // Hand copy. var hCopy = fieldsInstance.HCopy(); var hGetter = new ObjectGetter(fieldsType, hCopy); Assert.Equal("I'm private.", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(123, hGetter.FieldValue("PublicField")); // Deep copy. var dCopy = fieldsInstance.DeepCopy(); var dGetter = new ObjectGetter(fieldsType, dCopy); Assert.Equal("I'm private.", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(123, dGetter.FieldValue("PublicField")); }
public void ObjectDictionaryTest() { var fieldsType = this._assembly.GetType("AssemblyToProcess.Basic.Fields"); dynamic fieldInstanceOne = this.GetFieldInstance("first", 1); dynamic fieldInstanceTwo = this.GetFieldInstance("second", 2); dynamic fieldInstanceThree = this.GetFieldInstance("third", 3); var dictionaryType = Assembly.GetAssembly(typeof (Dictionary<Fields, Fields>)) .GetType("System.Collections.Generic.Dictionary`2"); var dictionaryInstance = (dynamic) Activator .CreateInstance(dictionaryType.MakeGenericType(fieldsType, fieldsType)); dynamic dictionaryAdd = dictionaryInstance.GetType().GetMethod("Add"); dictionaryAdd.Invoke(dictionaryInstance, new[] {fieldInstanceOne, fieldInstanceOne}); dictionaryAdd.Invoke(dictionaryInstance, new[] {fieldInstanceTwo, fieldInstanceTwo}); dictionaryAdd.Invoke(dictionaryInstance, new[] {fieldInstanceThree, null}); var hasDictionaryType = this._assembly.GetType("AssemblyToProcess.Enumerables.HasDictionary"); var hasDictionaryInstance = (dynamic) Activator.CreateInstance(hasDictionaryType); hasDictionaryType.GetField("DictionaryOfObjects").SetValue(hasDictionaryInstance, dictionaryInstance); // Hand copy. var hCopy = hasDictionaryInstance.HCopy(); foreach (var item in hCopy.DictionaryOfObjects) { ObjectGetter hGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields) item.Value); } } // Deep copy. dynamic dCopy = hasDictionaryInstance.DeepCopy(); foreach (var item in dCopy.DictionaryOfObjects) { ObjectGetter dGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields) item.Value); } } // Bad copy. dynamic bCopy = hasDictionaryInstance.BCopy(); foreach (var item in bCopy.DictionaryOfObjects) { ObjectGetter bGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, bGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, bGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields) item.Value); } } // Modify. dynamic dictionaryClear = dictionaryInstance.GetType().GetMethod("Clear"); dictionaryClear.Invoke(dictionaryInstance, new dynamic[0]); dictionaryAdd.Invoke(dictionaryInstance, new[] {fieldInstanceTwo, fieldInstanceOne}); dictionaryAdd.Invoke(dictionaryInstance, new[] {fieldInstanceThree, null}); // Hand copy. foreach (var item in hCopy.DictionaryOfObjects) { ObjectGetter hGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields) item.Value); } } // Deep copy. foreach (var item in dCopy.DictionaryOfObjects) { ObjectGetter dGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields) item.Value); } } // Bad copy. foreach (var item in bCopy.DictionaryOfObjects) { ObjectGetter bGetter = new ObjectGetter(fieldsType, item.Value); if (2 == item.Key.PublicField) { Assert.Equal("first", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, bGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields) item.Value); } } }
public void ArrayOfObjects() { var fieldsType = this._assembly.GetType("AssemblyToProcess.Basic.Fields"); dynamic fieldInstanceOne = this.GetFieldInstance("first", 1); dynamic fieldInstanceTwo = this.GetFieldInstance("second", 2); dynamic fieldsArrayInstance = Array.CreateInstance(fieldsType, 3); fieldsArrayInstance.SetValue(fieldInstanceOne, 0); fieldsArrayInstance.SetValue(fieldInstanceTwo, 1); fieldsArrayInstance.SetValue(null, 2); var arrayOfObjectsType = this._assembly .GetType("AssemblyToProcess.Arrays.ArrayOfObjects"); var arrayOfObjectsInstance = (dynamic) Activator.CreateInstance(arrayOfObjectsType); arrayOfObjectsType.GetField("FieldsArray").SetValue(arrayOfObjectsInstance, fieldsArrayInstance); // Hand copy. var hCopy = arrayOfObjectsInstance.HCopy(); var hGetter = new ObjectGetter(fieldsType, hCopy.FieldsArray[0]); Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); hGetter = new ObjectGetter(fieldsType, hCopy.FieldsArray[1]); Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)hCopy.FieldsArray[2]); // Deep copy. var dCopy = arrayOfObjectsInstance.DeepCopy(); var dGetter = new ObjectGetter(fieldsType, dCopy.FieldsArray[0]); Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); dGetter = new ObjectGetter(fieldsType, dCopy.FieldsArray[1]); Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)dCopy.FieldsArray[2]); // Bad copy. var bCopy = arrayOfObjectsInstance.BCopy(); var bGetter = new ObjectGetter(fieldsType, bCopy.FieldsArray[0]); Assert.Equal("first", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, bGetter.FieldValue("PublicField")); bGetter = new ObjectGetter(fieldsType, bCopy.FieldsArray[1]); Assert.Equal("second", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, bGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)bCopy.FieldsArray[2]); // Modify. fieldInstanceOne = this.GetFieldInstance("third", 3); fieldInstanceTwo = this.GetFieldInstance("fourth", 4); fieldsArrayInstance.SetValue(fieldInstanceOne, 0); fieldsArrayInstance.SetValue(fieldInstanceTwo, 1); arrayOfObjectsType.GetField("FieldsArray").SetValue(arrayOfObjectsInstance, fieldsArrayInstance); // Hand copy (should stay the same). hGetter = new ObjectGetter(fieldsType, hCopy.FieldsArray[0]); Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); hGetter = new ObjectGetter(fieldsType, hCopy.FieldsArray[1]); Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)hCopy.FieldsArray[2]); // Deep copy (should stay the same). dGetter = new ObjectGetter(fieldsType, dCopy.FieldsArray[0]); Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); dGetter = new ObjectGetter(fieldsType, dCopy.FieldsArray[1]); Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)dCopy.FieldsArray[2]); // Bad copy (should be modified). bGetter = new ObjectGetter(fieldsType, bCopy.FieldsArray[0]); Assert.Equal("third", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(3, bGetter.FieldValue("PublicField")); bGetter = new ObjectGetter(fieldsType, bCopy.FieldsArray[1]); Assert.Equal("fourth", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(4, bGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)bCopy.FieldsArray[2]); }
public void ArrayOfObjects() { var fieldsType = this._assembly.GetType("AssemblyToProcess.Basic.Fields"); dynamic fieldInstanceOne = this.GetFieldInstance("first", 1); dynamic fieldInstanceTwo = this.GetFieldInstance("second", 2); dynamic fieldsArrayInstance = Array.CreateInstance(fieldsType, 3); fieldsArrayInstance.SetValue(fieldInstanceOne, 0); fieldsArrayInstance.SetValue(fieldInstanceTwo, 1); fieldsArrayInstance.SetValue(null, 2); var arrayOfObjectsType = this._assembly .GetType("AssemblyToProcess.Arrays.ArrayOfObjects"); var arrayOfObjectsInstance = (dynamic)Activator.CreateInstance(arrayOfObjectsType); arrayOfObjectsType.GetField("FieldsArray").SetValue(arrayOfObjectsInstance, fieldsArrayInstance); // Hand copy. var hCopy = arrayOfObjectsInstance.HCopy(); var hGetter = new ObjectGetter(fieldsType, hCopy.FieldsArray[0]); Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); hGetter = new ObjectGetter(fieldsType, hCopy.FieldsArray[1]); Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)hCopy.FieldsArray[2]); // Deep copy. var dCopy = arrayOfObjectsInstance.DeepCopy(); var dGetter = new ObjectGetter(fieldsType, dCopy.FieldsArray[0]); Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); dGetter = new ObjectGetter(fieldsType, dCopy.FieldsArray[1]); Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)dCopy.FieldsArray[2]); // Bad copy. var bCopy = arrayOfObjectsInstance.BCopy(); var bGetter = new ObjectGetter(fieldsType, bCopy.FieldsArray[0]); Assert.Equal("first", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, bGetter.FieldValue("PublicField")); bGetter = new ObjectGetter(fieldsType, bCopy.FieldsArray[1]); Assert.Equal("second", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, bGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)bCopy.FieldsArray[2]); // Modify. fieldInstanceOne = this.GetFieldInstance("third", 3); fieldInstanceTwo = this.GetFieldInstance("fourth", 4); fieldsArrayInstance.SetValue(fieldInstanceOne, 0); fieldsArrayInstance.SetValue(fieldInstanceTwo, 1); arrayOfObjectsType.GetField("FieldsArray").SetValue(arrayOfObjectsInstance, fieldsArrayInstance); // Hand copy (should stay the same). hGetter = new ObjectGetter(fieldsType, hCopy.FieldsArray[0]); Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); hGetter = new ObjectGetter(fieldsType, hCopy.FieldsArray[1]); Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)hCopy.FieldsArray[2]); // Deep copy (should stay the same). dGetter = new ObjectGetter(fieldsType, dCopy.FieldsArray[0]); Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); dGetter = new ObjectGetter(fieldsType, dCopy.FieldsArray[1]); Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)dCopy.FieldsArray[2]); // Bad copy (should be modified). bGetter = new ObjectGetter(fieldsType, bCopy.FieldsArray[0]); Assert.Equal("third", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(3, bGetter.FieldValue("PublicField")); bGetter = new ObjectGetter(fieldsType, bCopy.FieldsArray[1]); Assert.Equal("fourth", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(4, bGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)bCopy.FieldsArray[2]); }
public void ObjectListTest() { var fieldsType = this._assembly.GetType("AssemblyToProcess.Basic.Fields"); dynamic fieldInstanceOne = this.GetFieldInstance("first", 1); dynamic fieldInstanceTwo = this.GetFieldInstance("second", 2); var listType = Assembly.GetAssembly(typeof(List <>)) .GetType("System.Collections.Generic.List`1"); var listInstance = (dynamic)Activator.CreateInstance(listType.MakeGenericType(fieldsType)); dynamic listAdd = listInstance.GetType().GetMethod("Add"); listAdd.Invoke(listInstance, new[] { fieldInstanceOne }); listAdd.Invoke(listInstance, new[] { fieldInstanceTwo }); listAdd.Invoke(listInstance, new dynamic[] { null }); var hasListType = this._assembly.GetType("AssemblyToProcess.Enumerables.HasList"); var hasListInstance = (dynamic)Activator.CreateInstance(hasListType); hasListType.GetField("ListOfObjects").SetValue(hasListInstance, listInstance); // Hand copy. var hCopy = hasListInstance.HCopy(); var hGetter = new ObjectGetter(fieldsType, hCopy.ListOfObjects[0]); Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); hGetter = new ObjectGetter(fieldsType, hCopy.ListOfObjects[1]); Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)hCopy.ListOfObjects[2]); // Deep copy. var dCopy = hasListInstance.DeepCopy(); var dGetter = new ObjectGetter(fieldsType, dCopy.ListOfObjects[0]); Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); dGetter = new ObjectGetter(fieldsType, dCopy.ListOfObjects[1]); Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)dCopy.ListOfObjects[2]); // Bad copy. var bCopy = hasListInstance.BCopy(); var bGetter = new ObjectGetter(fieldsType, bCopy.ListOfObjects[0]); Assert.Equal("first", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, bGetter.FieldValue("PublicField")); bGetter = new ObjectGetter(fieldsType, bCopy.ListOfObjects[1]); Assert.Equal("second", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, bGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)bCopy.ListOfObjects[2]); // Modify. dynamic listClear = listInstance.GetType().GetMethod("Clear"); listClear.Invoke(listInstance, new dynamic[0]); listAdd.Invoke(listInstance, new[] { fieldInstanceTwo }); listAdd.Invoke(listInstance, new dynamic[] { null }); // Hand copy. hGetter = new ObjectGetter(fieldsType, hCopy.ListOfObjects[0]); Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); hGetter = new ObjectGetter(fieldsType, hCopy.ListOfObjects[1]); Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)hCopy.ListOfObjects[2]); // Deep copy. dGetter = new ObjectGetter(fieldsType, dCopy.ListOfObjects[0]); Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); dGetter = new ObjectGetter(fieldsType, dCopy.ListOfObjects[1]); Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)dCopy.ListOfObjects[2]); // Bad copy. bGetter = new ObjectGetter(fieldsType, bCopy.ListOfObjects[0]); Assert.Equal("second", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, bGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields)bCopy.ListOfObjects[1]); }
public void ObjectDictionaryTest() { var fieldsType = this._assembly.GetType("AssemblyToProcess.Basic.Fields"); dynamic fieldInstanceOne = this.GetFieldInstance("first", 1); dynamic fieldInstanceTwo = this.GetFieldInstance("second", 2); dynamic fieldInstanceThree = this.GetFieldInstance("third", 3); var dictionaryType = Assembly.GetAssembly(typeof(Dictionary <Fields, Fields>)) .GetType("System.Collections.Generic.Dictionary`2"); var dictionaryInstance = (dynamic)Activator .CreateInstance(dictionaryType.MakeGenericType(fieldsType, fieldsType)); dynamic dictionaryAdd = dictionaryInstance.GetType().GetMethod("Add"); dictionaryAdd.Invoke(dictionaryInstance, new[] { fieldInstanceOne, fieldInstanceOne }); dictionaryAdd.Invoke(dictionaryInstance, new[] { fieldInstanceTwo, fieldInstanceTwo }); dictionaryAdd.Invoke(dictionaryInstance, new[] { fieldInstanceThree, null }); var hasDictionaryType = this._assembly.GetType("AssemblyToProcess.Enumerables.HasDictionary"); var hasDictionaryInstance = (dynamic)Activator.CreateInstance(hasDictionaryType); hasDictionaryType.GetField("DictionaryOfObjects").SetValue(hasDictionaryInstance, dictionaryInstance); // Hand copy. var hCopy = hasDictionaryInstance.HCopy(); foreach (var item in hCopy.DictionaryOfObjects) { ObjectGetter hGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields)item.Value); } } // Deep copy. dynamic dCopy = hasDictionaryInstance.DeepCopy(); foreach (var item in dCopy.DictionaryOfObjects) { ObjectGetter dGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields)item.Value); } } // Bad copy. dynamic bCopy = hasDictionaryInstance.BCopy(); foreach (var item in bCopy.DictionaryOfObjects) { ObjectGetter bGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, bGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, bGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields)item.Value); } } // Modify. dynamic dictionaryClear = dictionaryInstance.GetType().GetMethod("Clear"); dictionaryClear.Invoke(dictionaryInstance, new dynamic[0]); dictionaryAdd.Invoke(dictionaryInstance, new[] { fieldInstanceTwo, fieldInstanceOne }); dictionaryAdd.Invoke(dictionaryInstance, new[] { fieldInstanceThree, null }); // Hand copy. foreach (var item in hCopy.DictionaryOfObjects) { ObjectGetter hGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields)item.Value); } } // Deep copy. foreach (var item in dCopy.DictionaryOfObjects) { ObjectGetter dGetter = new ObjectGetter(fieldsType, item.Value); if (1 == item.Key.PublicField) { Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); } else if (2 == item.Key.PublicField) { Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields)item.Value); } } // Bad copy. foreach (var item in bCopy.DictionaryOfObjects) { ObjectGetter bGetter = new ObjectGetter(fieldsType, item.Value); if (2 == item.Key.PublicField) { Assert.Equal("first", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, bGetter.FieldValue("PublicField")); } else { // Are nulls handled properly? Assert.Equal(null, (Fields)item.Value); } } }
public void ObjectListTest() { var fieldsType = this._assembly.GetType("AssemblyToProcess.Basic.Fields"); dynamic fieldInstanceOne = this.GetFieldInstance("first", 1); dynamic fieldInstanceTwo = this.GetFieldInstance("second", 2); var listType = Assembly.GetAssembly(typeof (List<>)) .GetType("System.Collections.Generic.List`1"); var listInstance = (dynamic) Activator.CreateInstance(listType.MakeGenericType(fieldsType)); dynamic listAdd = listInstance.GetType().GetMethod("Add"); listAdd.Invoke(listInstance, new[] {fieldInstanceOne}); listAdd.Invoke(listInstance, new[] {fieldInstanceTwo}); listAdd.Invoke(listInstance, new dynamic[] {null}); var hasListType = this._assembly.GetType("AssemblyToProcess.Enumerables.HasList"); var hasListInstance = (dynamic) Activator.CreateInstance(hasListType); hasListType.GetField("ListOfObjects").SetValue(hasListInstance, listInstance); // Hand copy. var hCopy = hasListInstance.HCopy(); var hGetter = new ObjectGetter(fieldsType, hCopy.ListOfObjects[0]); Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); hGetter = new ObjectGetter(fieldsType, hCopy.ListOfObjects[1]); Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields) hCopy.ListOfObjects[2]); // Deep copy. var dCopy = hasListInstance.DeepCopy(); var dGetter = new ObjectGetter(fieldsType, dCopy.ListOfObjects[0]); Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); dGetter = new ObjectGetter(fieldsType, dCopy.ListOfObjects[1]); Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields) dCopy.ListOfObjects[2]); // Bad copy. var bCopy = hasListInstance.BCopy(); var bGetter = new ObjectGetter(fieldsType, bCopy.ListOfObjects[0]); Assert.Equal("first", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, bGetter.FieldValue("PublicField")); bGetter = new ObjectGetter(fieldsType, bCopy.ListOfObjects[1]); Assert.Equal("second", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, bGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields) bCopy.ListOfObjects[2]); // Modify. dynamic listClear = listInstance.GetType().GetMethod("Clear"); listClear.Invoke(listInstance, new dynamic[0]); listAdd.Invoke(listInstance, new[] {fieldInstanceTwo}); listAdd.Invoke(listInstance, new dynamic[] {null}); // Hand copy. hGetter = new ObjectGetter(fieldsType, hCopy.ListOfObjects[0]); Assert.Equal("first", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, hGetter.FieldValue("PublicField")); hGetter = new ObjectGetter(fieldsType, hCopy.ListOfObjects[1]); Assert.Equal("second", hGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, hGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields) hCopy.ListOfObjects[2]); // Deep copy. dGetter = new ObjectGetter(fieldsType, dCopy.ListOfObjects[0]); Assert.Equal("first", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(1, dGetter.FieldValue("PublicField")); dGetter = new ObjectGetter(fieldsType, dCopy.ListOfObjects[1]); Assert.Equal("second", dGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, dGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields) dCopy.ListOfObjects[2]); // Bad copy. bGetter = new ObjectGetter(fieldsType, bCopy.ListOfObjects[0]); Assert.Equal("second", bGetter.PrivateFieldValue("_privateField")); Assert.Equal(2, bGetter.FieldValue("PublicField")); // Are nulls handled properly? Assert.Equal(null, (Fields) bCopy.ListOfObjects[1]); }