コード例 #1
0
        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"));
        }
コード例 #2
0
        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"));
        }
コード例 #3
0
        public void ObjectInheritanceTest()
        {
            var inheritsAbstractType     = this._assembly.GetType("AssemblyToProcess.Abstracts.InheritsAbstract");
            var inheritsAbstractInstance = (dynamic)Activator.CreateInstance(inheritsAbstractType);

            inheritsAbstractType.GetField("MainObject").SetValue(inheritsAbstractInstance, "main obj");
            inheritsAbstractType.GetProperty("InterfaceProperty").SetValue(inheritsAbstractInstance, 123);

            // Hand copy.
            var hCopy = inheritsAbstractInstance.HCopy();

            var hGet = new ObjectGetter(inheritsAbstractType, hCopy);

            Assert.Equal("main obj", hGet.FieldValue("MainObject"));
            Assert.Equal(123, hGet.PropertyValue("InterfaceProperty"));

            // Deep copy.
            var dCopy = inheritsAbstractInstance.DeepCopy();

            var dGet = new ObjectGetter(inheritsAbstractType, dCopy);

            Assert.Equal("main obj", dGet.FieldValue("MainObject"));
            Assert.Equal(123, dGet.PropertyValue("InterfaceProperty"));
        }
コード例 #4
0
        public void ObjectInheritanceTest()
        {
            var inheritsAbstractType = this._assembly.GetType("AssemblyToProcess.Abstracts.InheritsAbstract");
            var inheritsAbstractInstance = (dynamic) Activator.CreateInstance(inheritsAbstractType);

            inheritsAbstractType.GetField("MainObject").SetValue(inheritsAbstractInstance, "main obj");
            inheritsAbstractType.GetProperty("InterfaceProperty").SetValue(inheritsAbstractInstance, 123);

            // Hand copy.
            var hCopy = inheritsAbstractInstance.HCopy();

            var hGet = new ObjectGetter(inheritsAbstractType, hCopy);

            Assert.Equal("main obj", hGet.FieldValue("MainObject"));
            Assert.Equal(123, hGet.PropertyValue("InterfaceProperty"));

            // Deep copy.
            var dCopy = inheritsAbstractInstance.DeepCopy();

            var dGet = new ObjectGetter(inheritsAbstractType, dCopy);

            Assert.Equal("main obj", dGet.FieldValue("MainObject"));
            Assert.Equal(123, dGet.PropertyValue("InterfaceProperty"));
        }
コード例 #5
0
        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);
                }
            }
        }
コード例 #6
0
        public void Tests()
        {
            var propT = this._assembly.GetType("AssemblyToProcess.Basic.Properties");
            var propI = (dynamic)Activator.CreateInstance(propT);

            PropertyExtensions.SetPrivatePropertyValue(propI, "BackedProperty", 1.23);
            PropertyExtensions.SetPrivatePropertyValue(propI, "PrivateProperty", 123);
            propT.GetProperty("PublicProperty").SetValue(propI, "I'm public.");

            var typicalIT = this._assembly.GetType("AssemblyToProcess.Inherits.TypicalInheritance");
            var typicalII = (dynamic)Activator.CreateInstance(typicalIT);

            typicalIT.GetField("Properties").SetValue(typicalII, propI);

            // Hand copy.
            var hCopy = typicalII.HCopy();
            var hGet  = new ObjectGetter(typicalIT, hCopy);

            var fValue = hGet.FieldValue("Properties");

            hGet = new ObjectGetter(propT, fValue);

            Assert.Equal(1.23, hGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(0, hGet.PrivatePropertyValue("PrivateProperty")); // Won't copy since it's private.
            Assert.Equal("I'm public.", hGet.PropertyValue("PublicProperty"));

            // Deep copy.
            var dCopy = typicalII.DeepCopy();
            var dGet  = new ObjectGetter(typicalIT, dCopy);

            fValue = dGet.FieldValue("Properties");
            dGet   = new ObjectGetter(propT, fValue);

            Assert.Equal(1.23, dGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(123, dGet.PrivatePropertyValue("PrivateProperty"));
            Assert.Equal("I'm public.", dGet.PropertyValue("PublicProperty"));

            // Bad copy.
            var bCopy = typicalII.BCopy();
            var bGet  = new ObjectGetter(typicalIT, bCopy);

            fValue = bGet.FieldValue("Properties");
            bGet   = new ObjectGetter(propT, fValue);

            Assert.Equal(1.23, bGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(123, bGet.PrivatePropertyValue("PrivateProperty"));
            Assert.Equal("I'm public.", bGet.PropertyValue("PublicProperty"));

            // Modify.
            PropertyExtensions.SetPrivatePropertyValue(propI, "BackedProperty", 2.34);
            PropertyExtensions.SetPrivatePropertyValue(propI, "PrivateProperty", 234);
            propT.GetProperty("PublicProperty").SetValue(propI, "I'm changed!");
            typicalIT.GetField("Properties").SetValue(typicalII, propI);

            // Hand copy.
            hGet = new ObjectGetter(typicalIT, hCopy);

            fValue = hGet.FieldValue("Properties");
            hGet   = new ObjectGetter(propT, fValue);

            Assert.Equal(1.23, hGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(0, hGet.PrivatePropertyValue("PrivateProperty")); // Won't copy since it's private.
            Assert.Equal("I'm public.", hGet.PropertyValue("PublicProperty"));

            // Deep copy.
            dGet = new ObjectGetter(typicalIT, dCopy);

            fValue = dGet.FieldValue("Properties");
            dGet   = new ObjectGetter(propT, fValue);

            Assert.Equal(1.23, dGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(123, dGet.PrivatePropertyValue("PrivateProperty"));
            Assert.Equal("I'm public.", dGet.PropertyValue("PublicProperty"));

            // Bad copy.
            bGet = new ObjectGetter(typicalIT, bCopy);

            fValue = bGet.FieldValue("Properties");
            bGet   = new ObjectGetter(propT, fValue);

            Assert.Equal(2.34, bGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(234, bGet.PrivatePropertyValue("PrivateProperty"));
            Assert.Equal("I'm changed!", bGet.PropertyValue("PublicProperty"));
        }
コード例 #7
0
        public void Tests()
        {
            var propT = this._assembly.GetType("AssemblyToProcess.Basic.Properties");
            var propI = (dynamic) Activator.CreateInstance(propT);

            PropertyExtensions.SetPrivatePropertyValue(propI, "BackedProperty", 1.23);
            PropertyExtensions.SetPrivatePropertyValue(propI, "PrivateProperty", 123);
            propT.GetProperty("PublicProperty").SetValue(propI, "I'm public.");

            var typicalIT = this._assembly.GetType("AssemblyToProcess.Inherits.TypicalInheritance");
            var typicalII = (dynamic) Activator.CreateInstance(typicalIT);

            typicalIT.GetField("Properties").SetValue(typicalII, propI);

            // Hand copy.
            var hCopy = typicalII.HCopy();
            var hGet = new ObjectGetter(typicalIT, hCopy);

            var fValue = hGet.FieldValue("Properties");
            hGet = new ObjectGetter(propT, fValue);

            Assert.Equal(1.23, hGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(0, hGet.PrivatePropertyValue("PrivateProperty")); // Won't copy since it's private.
            Assert.Equal("I'm public.", hGet.PropertyValue("PublicProperty"));

            // Deep copy.
            var dCopy = typicalII.DeepCopy();
            var dGet = new ObjectGetter(typicalIT, dCopy);

            fValue = dGet.FieldValue("Properties");
            dGet = new ObjectGetter(propT, fValue);

            Assert.Equal(1.23, dGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(123, dGet.PrivatePropertyValue("PrivateProperty"));
            Assert.Equal("I'm public.", dGet.PropertyValue("PublicProperty"));

            // Bad copy.
            var bCopy = typicalII.BCopy();
            var bGet = new ObjectGetter(typicalIT, bCopy);

            fValue = bGet.FieldValue("Properties");
            bGet = new ObjectGetter(propT, fValue);
 
            Assert.Equal(1.23, bGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(123, bGet.PrivatePropertyValue("PrivateProperty"));
            Assert.Equal("I'm public.", bGet.PropertyValue("PublicProperty"));
            
            // Modify.
            PropertyExtensions.SetPrivatePropertyValue(propI, "BackedProperty", 2.34);
            PropertyExtensions.SetPrivatePropertyValue(propI, "PrivateProperty", 234);
            propT.GetProperty("PublicProperty").SetValue(propI, "I'm changed!");
            typicalIT.GetField("Properties").SetValue(typicalII, propI);

            // Hand copy.
            hGet = new ObjectGetter(typicalIT, hCopy);

            fValue = hGet.FieldValue("Properties");
            hGet = new ObjectGetter(propT, fValue);

            Assert.Equal(1.23, hGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(0, hGet.PrivatePropertyValue("PrivateProperty")); // Won't copy since it's private.
            Assert.Equal("I'm public.", hGet.PropertyValue("PublicProperty"));

            // Deep copy.
            dGet = new ObjectGetter(typicalIT, dCopy);

            fValue = dGet.FieldValue("Properties");
            dGet = new ObjectGetter(propT, fValue);

            Assert.Equal(1.23, dGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(123, dGet.PrivatePropertyValue("PrivateProperty"));
            Assert.Equal("I'm public.", dGet.PropertyValue("PublicProperty"));

            // Bad copy.
            bGet = new ObjectGetter(typicalIT, bCopy);

            fValue = bGet.FieldValue("Properties");
            bGet = new ObjectGetter(propT, fValue);
 
            Assert.Equal(2.34, bGet.PrivatePropertyValue("BackedProperty"));
            Assert.Equal(234, bGet.PrivatePropertyValue("PrivateProperty"));
            Assert.Equal("I'm changed!", bGet.PropertyValue("PublicProperty"));
        }
コード例 #8
0
        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]);
        }
コード例 #9
0
        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]);
        }
コード例 #10
0
        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]);
        }
コード例 #11
0
        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);
                }
            }
        }
コード例 #12
0
        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]);
        }