public void TestInstanceMembersOfClass() { object testref1 = new object(); object testref2 = new object(); MyClassWithPrivateFields myClass; // ro readonly int myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2); SafeField instanceReadonlyVtField = new SafeField(IField(myClass.GetType(), "instanceReadonlyVTValue")); Assert.AreEqual(123, instanceReadonlyVtField.GetValue(myClass)); try { instanceReadonlyVtField.SetValue(myClass, 10); } catch (InvalidOperationException) { } // ro readonly object myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2); SafeField instanceReadonlyRefField = new SafeField(IField(myClass.GetType(), "instanceReadonlyRefValue")); Assert.AreSame(testref1, instanceReadonlyRefField.GetValue(myClass)); try { instanceReadonlyRefField.SetValue(myClass, this); } catch (InvalidOperationException) { } // rw int myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2); SafeField instanceVtField = new SafeField(IField(myClass.GetType(), "instanceVTValue")); Assert.AreEqual(456, instanceVtField.GetValue(myClass)); instanceVtField.SetValue(myClass, 9182); Assert.AreEqual(9182, instanceVtField.GetValue(myClass)); // rw object myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2); SafeField instanceRefField = new SafeField(IField(myClass.GetType(), "instanceRefValue")); Assert.AreSame(testref2, instanceRefField.GetValue(myClass)); instanceRefField.SetValue(myClass, testref1); Assert.AreSame(testref1, instanceRefField.GetValue(myClass)); }
public void TestInstanceMembersOfStruct() { object testref1 = new object(); object testref2 = new object(); MyStructWithPrivateFields myStruct; // ro readonly int myStruct = new MyStructWithPrivateFields(123, testref1, 456, testref2); SafeField instanceReadonlyVtField = new SafeField(IField(myStruct.GetType(), "instanceReadonlyVTValue")); Assert.AreEqual(123, instanceReadonlyVtField.GetValue(myStruct)); try { instanceReadonlyVtField.SetValue(myStruct, 10); } catch (InvalidOperationException) { } // ro readonly object myStruct = new MyStructWithPrivateFields(123, testref1, 456, testref2); SafeField instanceReadonlyRefField = new SafeField(IField(myStruct.GetType(), "instanceReadonlyRefValue")); Assert.AreSame(testref1, instanceReadonlyRefField.GetValue(myStruct)); try { instanceReadonlyRefField.SetValue(myStruct, this); } catch (InvalidOperationException) { } // ro int myStruct = new MyStructWithPrivateFields(123, testref1, 456, testref2); SafeField instanceVtField = new SafeField(IField(myStruct.GetType(), "instanceVTValue")); Assert.AreEqual(456, instanceVtField.GetValue(myStruct)); try { instanceVtField.SetValue(myStruct, 10); } catch (InvalidOperationException) { } // ro object myStruct = new MyStructWithPrivateFields(123, testref1, 456, testref2); SafeField instanceRefField = new SafeField(IField(myStruct.GetType(), "instanceRefValue")); Assert.AreSame(testref2, instanceRefField.GetValue(myStruct)); try { instanceRefField.SetValue(myStruct, 10); } catch (InvalidOperationException) { } }
private void TestStaticMembersOf(Type type, object expectedRoRefValue) { // ro const int SafeField constField = new SafeField(SField(type, "constantValue")); Assert.AreEqual(5, constField.GetValue(null)); try { constField.SetValue(null, 3); } catch (InvalidOperationException) { } // ro static readonly int SafeField roVtField = new SafeField(SField(type, "staticReadonlyVTValue")); Assert.AreEqual(11, roVtField.GetValue(null)); try { roVtField.SetValue(null, 10); } catch (InvalidOperationException) { } // ro static readonly object SafeField roRefField = new SafeField(SField(type, "staticReadonlyRefValue")); Assert.AreSame(expectedRoRefValue, roRefField.GetValue(null)); try { roRefField.SetValue(null, new object()); } catch (InvalidOperationException) { } // rw static int SafeField vtField = new SafeField(SField(type, "staticVTValue")); vtField.SetValue(null, 10); Assert.AreEqual(10, vtField.GetValue(null)); // rw static object SafeField refField = new SafeField(SField(type, "staticRefValue")); object o = new object(); refField.SetValue(null, o); Assert.AreSame(o, refField.GetValue(null)); }