public void ImmediateFieldSetValue_Constant()
        {
            var testObject = new PublicValueTypeTestClass();

            // Static readonly
            var immediateField = new ImmediateField(PublicValueTypeStaticReadonlyPublicFieldFieldsInfo);

            Assert.Throws <FieldAccessException>(() => immediateField.SetValue(testObject, 123456));
            Assert.Throws <FieldAccessException>(() => immediateField.SetValue(null, 123456));

            // Constant
            immediateField = new ImmediateField(PublicValueTypeConstPublicFieldFieldsInfo);
            Assert.Throws <FieldAccessException>(() => immediateField.SetValue(testObject, 123456));
            Assert.Throws <FieldAccessException>(() => immediateField.SetValue(null, 123456));
        }
        public void ImmediateFieldSetValue_NullInstance()
        {
            var immediateField = new ImmediateField(PublicValueTypePublicFieldFieldsInfo);

            // ReSharper disable once AssignNullToNotNullAttribute
            Assert.Throws <TargetException>(() => immediateField.SetValue(null, null));
        }
        public void ImmediateFieldSetValue_NestedTypes()
        {
            var publicNestedTypeTestObject = new PublicTestClass.PublicNestedClass {
                _nestedTestValue = 1
            };
            var immediateField = new ImmediateField(PublicNestedPublicFieldFieldInfo);

            immediateField.SetValue(publicNestedTypeTestObject, 12);
            Assert.AreEqual(12, publicNestedTypeTestObject._nestedTestValue);

            var internalNestedTypeTestObject = new PublicTestClass.InternalNestedClass {
                _nestedTestValue = 2
            };

            immediateField = new ImmediateField(InternalNestedPublicFieldFieldInfo);
            immediateField.SetValue(internalNestedTypeTestObject, 24);
            Assert.AreEqual(24, internalNestedTypeTestObject._nestedTestValue);

            var protectedNestedTypeTestObject = new ProtectedNestedClass {
                _nestedTestValue = 3
            };

            immediateField = new ImmediateField(ProtectedNestedPublicFieldFieldInfo);
            immediateField.SetValue(protectedNestedTypeTestObject, 48);
            Assert.AreEqual(48, protectedNestedTypeTestObject._nestedTestValue);

            var privateNestedTypeTestObject = new PrivateNestedClass {
                _nestedTestValue = 4
            };

            immediateField = new ImmediateField(PrivateNestedPublicFieldFieldInfo);
            immediateField.SetValue(privateNestedTypeTestObject, 96);
            Assert.AreEqual(96, privateNestedTypeTestObject._nestedTestValue);
        }
 public void SetImmediateField_Field()
 {
     ImmediateField.SetValue(BenchmarkObject, ValueToSet);
     ImmediateField2.SetValue(BenchmarkObject2, ValueToSet2);
     ImmediateField3.SetValue(BenchmarkObject3, ValueToSet3);
     ImmediateField4.SetValue(BenchmarkObject4, ValueToSet4);
 }
        public void ImmediateFieldSetValue_Enum()
        {
            TestEnum testEnum = TestEnum.EnumValue2;

            var immediateField = new ImmediateField(TestEnumFieldValueFieldInfo);

            immediateField.SetValue(testEnum, TestEnum.EnumValue1);
            Assert.AreEqual(TestEnum.EnumValue2, testEnum);     // Enum internal value cannot be set
        }
        public void ImmediateFieldSetValue_EnumThrows()
        {
            var immediateField = new ImmediateField(TestEnumFieldValueFieldInfo);

            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <TargetException>(() => immediateField.SetValue(null, TestEnum.EnumValue2));

            immediateField = new ImmediateField(TestEnumField1FieldInfo);
            Assert.Throws <FieldAccessException>(() => immediateField.SetValue(null, TestEnum.EnumValue2));
        }
        public void ImmediateFieldSetValue_Struct()
        {
            var testStruct = new TestStruct();

            var immediateField = new ImmediateField(TestStructTestFieldFieldInfo);

            immediateField.SetValue(testStruct, 45);
            Assert.AreEqual(0, testStruct._testValue);  // Not updated there (but on the shadow copy yes) since struct are immutable
            // Limitation is the same with classic FieldInfo
        }
        public void ImmediateFieldSetValue_WrongValue()
        {
            var immediateField1 = new ImmediateField(PublicValueTypePublicFieldFieldsInfo);

            Assert.Throws <InvalidCastException>(() => immediateField1.SetValue(new PublicValueTypeTestClass(), new TestObject()));

            var immediateField2 = new ImmediateField(PublicReferenceTypePublicFieldFieldsInfo);

            Assert.Throws <InvalidCastException>(() => immediateField2.SetValue(new PublicReferenceTypeTestClass(), 12));

            var immediateField3 = new ImmediateField(PublicReferenceTypePublicFieldFieldsInfo);

            Assert.Throws <InvalidCastException>(() => immediateField3.SetValue(new PublicReferenceTypeTestClass(), new SmallObject()));
        }
        public void ImmediateFieldSetValue_Static()
        {
            var testObject = new TestObject {
                TestValue = 1
            };

            // Value type
            var immediateProperty = new ImmediateField(PublicValueTypeStaticPublicFieldFieldsInfo);

            immediateProperty.SetValue(null, 12);
            Assert.AreEqual(12, PublicValueTypeTestClass._publicStaticField);

            immediateProperty = new ImmediateField(InternalValueTypeStaticPublicFieldFieldsInfo);
            immediateProperty.SetValue(null, 24);
            Assert.AreEqual(24, InternalValueTypeTestClass._publicStaticField);

            // Reference type
            immediateProperty = new ImmediateField(PublicReferenceTypeStaticPublicFieldFieldsInfo);
            immediateProperty.SetValue(null, testObject);
            Assert.AreSame(testObject, PublicReferenceTypeTestClass._publicStaticField);

            immediateProperty = new ImmediateField(InternalReferenceTypeStaticPublicFieldFieldsInfo);
            immediateProperty.SetValue(null, testObject);
            Assert.AreSame(testObject, InternalReferenceTypeTestClass._publicStaticField);

            // Object type
            immediateProperty = new ImmediateField(PublicObjectTypeStaticPublicFieldFieldsInfo);
            immediateProperty.SetValue(null, 48);
            Assert.AreEqual(48, PublicObjectTypeTestClass._publicStaticField);

            immediateProperty = new ImmediateField(InternalObjectTypeStaticPublicFieldFieldsInfo);
            immediateProperty.SetValue(null, 96);
            Assert.AreEqual(96, InternalObjectTypeTestClass._publicStaticField);

            immediateProperty = new ImmediateField(PublicObjectTypeStaticPublicFieldFieldsInfo);
            immediateProperty.SetValue(null, testObject);
            Assert.AreSame(testObject, PublicObjectTypeTestClass._publicStaticField);

            immediateProperty = new ImmediateField(InternalObjectTypeStaticPublicFieldFieldsInfo);
            immediateProperty.SetValue(null, testObject);
            Assert.AreSame(testObject, InternalObjectTypeTestClass._publicStaticField);
        }
        public void ImmediateFieldSetValue_ValueType()
        {
            // Value type / Public
            var publicValueTypeTestObject = new PublicValueTypeTestClass();

            var immediateField = new ImmediateField(PublicValueTypePublicFieldFieldsInfo);

            immediateField.SetValue(publicValueTypeTestObject, 12);
            Assert.AreEqual(12, publicValueTypeTestObject._publicField);

            immediateField = new ImmediateField(PublicValueTypeInternalFieldFieldsInfo);
            immediateField.SetValue(publicValueTypeTestObject, 24);
            Assert.AreEqual(24, publicValueTypeTestObject._internalField);

            immediateField = new ImmediateField(PublicValueTypeProtectedFieldFieldsInfo);
            immediateField.SetValue(publicValueTypeTestObject, 48);
            Assert.AreEqual(48, publicValueTypeTestObject.GetProtectedFieldValue());

            immediateField = new ImmediateField(PublicValueTypePrivateFieldFieldsInfo);
            immediateField.SetValue(publicValueTypeTestObject, 96);
            Assert.AreEqual(96, publicValueTypeTestObject.GetPrivateFieldValue());

            // Value type / Internal
            var internalValueTypeTestObject = new InternalValueTypeTestClass();

            immediateField = new ImmediateField(InternalValueTypePublicFieldFieldsInfo);
            immediateField.SetValue(internalValueTypeTestObject, 192);
            Assert.AreEqual(192, internalValueTypeTestObject._publicField);

            immediateField = new ImmediateField(InternalValueTypeInternalFieldFieldsInfo);
            immediateField.SetValue(internalValueTypeTestObject, 384);
            Assert.AreEqual(384, internalValueTypeTestObject._internalField);

            immediateField = new ImmediateField(InternalValueTypeProtectedFieldFieldsInfo);
            immediateField.SetValue(internalValueTypeTestObject, 728);
            Assert.AreEqual(728, internalValueTypeTestObject.GetProtectedFieldValue());

            immediateField = new ImmediateField(InternalValueTypePrivateFieldFieldsInfo);
            immediateField.SetValue(internalValueTypeTestObject, 1536);
            Assert.AreEqual(1536, internalValueTypeTestObject.GetPrivateFieldValue());
        }
        public void ImmediateFieldSetValue_WrongInstance()
        {
            var immediateField = new ImmediateField(PublicValueTypePublicFieldFieldsInfo);

            Assert.Throws <InvalidCastException>(() => immediateField.SetValue(new PublicReferenceTypeTestClass(), null));
        }
        public void ImmediateFieldSetValue_ObjectType()
        {
            var testObject1 = new TestObject {
                TestValue = 1
            };
            var testObject2 = new TestObject {
                TestValue = 2
            };
            var testObject3 = new TestObject {
                TestValue = 3
            };
            var testObject4 = new TestObject {
                TestValue = 4
            };
            var testObject5 = new TestObject {
                TestValue = 5
            };
            var testObject6 = new TestObject {
                TestValue = 6
            };
            var testObject7 = new TestObject {
                TestValue = 7
            };
            var testObject8 = new TestObject {
                TestValue = 8
            };

            // Object type / Public
            var publicObjectTypeTestObject = new PublicObjectTypeTestClass();

            var immediateField = new ImmediateField(PublicObjectTypePublicFieldFieldsInfo);

            immediateField.SetValue(publicObjectTypeTestObject, 1);
            Assert.AreEqual(1, publicObjectTypeTestObject._publicField);

            immediateField = new ImmediateField(PublicObjectTypeInternalFieldFieldsInfo);
            immediateField.SetValue(publicObjectTypeTestObject, 2);
            Assert.AreEqual(2, publicObjectTypeTestObject._internalField);

            immediateField = new ImmediateField(PublicObjectTypeProtectedFieldFieldsInfo);
            immediateField.SetValue(publicObjectTypeTestObject, 3);
            Assert.AreEqual(3, publicObjectTypeTestObject.GetProtectedFieldValue());

            immediateField = new ImmediateField(PublicObjectTypePrivateFieldFieldsInfo);
            immediateField.SetValue(publicObjectTypeTestObject, 4);
            Assert.AreEqual(4, publicObjectTypeTestObject.GetPrivateFieldValue());


            immediateField = new ImmediateField(PublicObjectTypePublicFieldFieldsInfo);
            immediateField.SetValue(publicObjectTypeTestObject, testObject1);
            Assert.AreSame(testObject1, publicObjectTypeTestObject._publicField);

            immediateField = new ImmediateField(PublicObjectTypeInternalFieldFieldsInfo);
            immediateField.SetValue(publicObjectTypeTestObject, testObject2);
            Assert.AreSame(testObject2, publicObjectTypeTestObject._internalField);

            immediateField = new ImmediateField(PublicObjectTypeProtectedFieldFieldsInfo);
            immediateField.SetValue(publicObjectTypeTestObject, testObject3);
            Assert.AreSame(testObject3, publicObjectTypeTestObject.GetProtectedFieldValue());

            immediateField = new ImmediateField(PublicObjectTypePrivateFieldFieldsInfo);
            immediateField.SetValue(publicObjectTypeTestObject, testObject4);
            Assert.AreSame(testObject4, publicObjectTypeTestObject.GetPrivateFieldValue());


            // Object type / Internal
            var internalObjectTypeTestObject = new InternalObjectTypeTestClass();

            immediateField = new ImmediateField(InternalObjectTypePublicFieldFieldsInfo);
            immediateField.SetValue(internalObjectTypeTestObject, 1);
            Assert.AreEqual(1, internalObjectTypeTestObject._publicField);

            immediateField = new ImmediateField(InternalObjectTypeInternalFieldFieldsInfo);
            immediateField.SetValue(internalObjectTypeTestObject, 2);
            Assert.AreEqual(2, internalObjectTypeTestObject._internalField);

            immediateField = new ImmediateField(InternalObjectTypeProtectedFieldFieldsInfo);
            immediateField.SetValue(internalObjectTypeTestObject, 3);
            Assert.AreEqual(3, internalObjectTypeTestObject.GetProtectedFieldValue());

            immediateField = new ImmediateField(InternalObjectTypePrivateFieldFieldsInfo);
            immediateField.SetValue(internalObjectTypeTestObject, 4);
            Assert.AreEqual(4, internalObjectTypeTestObject.GetPrivateFieldValue());


            immediateField = new ImmediateField(InternalObjectTypePublicFieldFieldsInfo);
            immediateField.SetValue(internalObjectTypeTestObject, testObject5);
            Assert.AreSame(testObject5, internalObjectTypeTestObject._publicField);

            immediateField = new ImmediateField(InternalObjectTypeInternalFieldFieldsInfo);
            immediateField.SetValue(internalObjectTypeTestObject, testObject6);
            Assert.AreSame(testObject6, internalObjectTypeTestObject._internalField);

            immediateField = new ImmediateField(InternalObjectTypeProtectedFieldFieldsInfo);
            immediateField.SetValue(internalObjectTypeTestObject, testObject7);
            Assert.AreSame(testObject7, internalObjectTypeTestObject.GetProtectedFieldValue());

            immediateField = new ImmediateField(InternalObjectTypePrivateFieldFieldsInfo);
            immediateField.SetValue(internalObjectTypeTestObject, testObject8);
            Assert.AreSame(testObject8, internalObjectTypeTestObject.GetPrivateFieldValue());
        }
예제 #13
0
 public void SetImmediateField_Field()
 {
     ImmediateField.SetValue(BenchmarkObject, ValueToSet);
 }