Example #1
0
        public void SetField()
        {
            var obj = new ReflectionExampleClass();

            ReflectionUtility.SetField(obj, nameof(ReflectionExampleClass.PublicField), "publicValue");
            Assert.AreEqual("publicValue", obj.PublicField);
            obj.SetField(nameof(ReflectionExampleClass.InternalField), "internalValue");
            Assert.AreEqual("internalValue", obj.InternalField);
            obj.SetField(nameof(ReflectionExampleClass.ProtectedInternalField), "protectedInternalValue");
            Assert.AreEqual("protectedInternalValue", obj.ProtectedInternalField);
            obj.SetField("ProtectedField", "protectedValue");
            Assert.AreEqual("protectedValue", obj.GetProtectedField);
            obj.SetField("privateField", "privateValue");
            Assert.AreEqual("privateValue", obj.GetPrivateField);
            obj.SetField("privateReadonlyField", "privateReadonlyValue");
            Assert.AreEqual("privateReadonlyValue", obj.GetPrivateReadonlyField);
        }
Example #2
0
        public void AreAllPropertiesEqual()
        {
            _ = Assert.ThrowsException <ArgumentNullException>(() => ReflectionUtility.AreAllPropertiesEqual(null));
            // at least two objects must be provided
            _ = Assert.ThrowsException <ArgumentOutOfRangeException>(() => ReflectionUtility.AreAllPropertiesEqual(new object[0]));
            _ = Assert.ThrowsException <ArgumentOutOfRangeException>(() => ReflectionUtility.AreAllPropertiesEqual(new object[1]));
            // null objects are not allowed
            _ = Assert.ThrowsException <ArgumentException>(() => ReflectionUtility.AreAllPropertiesEqual(new object[2]));
            _ = Assert.ThrowsException <ArgumentException>(() => ReflectionUtility.AreAllPropertiesEqual(new ReflectionExampleClass[1024]));
            // all objects must be of the same type
            _ = Assert.ThrowsException <ArgumentException>(() => ReflectionUtility.AreAllPropertiesEqual(new object[2] {
                "string", 42
            }));
            // primitive types are not allowed
            _ = Assert.ThrowsException <ArgumentException>(() => ReflectionUtility.AreAllPropertiesEqual(new object[2] {
                "string", "string"
            }));

            var objects = new ReflectionExampleClass[2]
            {
                new ReflectionExampleClass
                {
                    // different field values and internal properties
                    PublicField      = "Public field 0",
                    InternalField    = "Internal field 0",
                    InternalProperty = "Internal property 0"
                },
                new ReflectionExampleClass
                {
                    // different field values and internal properties
                    PublicField      = "Public field 1",
                    InternalField    = "Internal field 1",
                    InternalProperty = "Internal property 1"
                }
            };

            // set different values to private and protected properties
            for (int i = 0; i < objects.Length; ++i)
            {
                objects[i].SetProperty("PrivateProperty", $"Private property {i}");
                objects[i].SetProperty("ProtectedProperty", $"Protected property {i}");
            }

            // all public properties are null (have not been set)
            Assert.IsTrue(ReflectionUtility.AreAllPropertiesEqual(objects));

            objects.ForEach(obj => obj.PublicProperty = "some value");
            Assert.IsTrue(ReflectionUtility.AreAllPropertiesEqual(objects));

            // set different public readonly properties
            for (int i = 0; i < objects.Length; ++i)
            {
                objects[i].SetField("privateField", $"Public readonly property {i}");
            }
            Assert.IsFalse(ReflectionUtility.AreAllPropertiesEqual(objects));

            // reset public readonly properties
            objects.ForEach(obj => obj.SetField("privateField", "same readonly value"));
            Assert.IsTrue(ReflectionUtility.AreAllPropertiesEqual(objects));

            objects[0].PublicProperty = "different value";
            Assert.IsFalse(ReflectionUtility.AreAllPropertiesEqual(objects));
        }