private static void NewMethod(Type classForMetaTestsType, ClassForMetaTests instance) { StartTest("Class get+invoke simple method via reflection"); var mtd = classForMetaTestsType.GetMethod("ReturnTrueIf1"); bool shouldBeTrue = (bool)mtd.Invoke(instance, new object[] { 1 }); bool shouldBeFalse = (bool)mtd.Invoke(instance, new object[] { 2 }); EndTest(shouldBeTrue && !shouldBeFalse); StartTest("Class get+invoke method with ref param via reflection"); var mtdWith2Params = classForMetaTestsType.GetMethod("ReturnTrueIf1AndThis"); shouldBeTrue = (bool)mtdWith2Params.Invoke(instance, new object[] { 1, instance }); shouldBeFalse = (bool)mtdWith2Params.Invoke(instance, new object[] { 1, new ClassForMetaTests() }); EndTest(shouldBeTrue && !shouldBeFalse); }
private static void TestMetaData() { StartTest("type == null. Simple class metadata test"); var typeGetType = Type.GetType("System.Char, System.Private.CoreLib"); if (typeGetType == null) { FailTest("type == null. Simple class metadata test"); } else { if (typeGetType.FullName != "System.Char") { FailTest("type != System.Char. Simple class metadata test"); } else { PassTest(); } } StartTest("Simple struct metadata test (typeof(Char))"); var typeofChar = typeof(Char); if (typeofChar == null) { FailTest("type == null. Simple struct metadata test"); } else { if (typeofChar.FullName != "System.Char") { FailTest("type != System.Char. Simple struct metadata test"); } else { PassTest(); } } var gentT = new Gen <int>(); var genParamType = gentT.TestTypeOf(); StartTest("type of generic parameter"); if (genParamType.FullName != "System.Int32") { FailTest("expected System.Int32 but was " + genParamType.FullName); } else { PassTest(); } var arrayType = typeof(object[]); StartTest("type of array"); if (arrayType.FullName != "System.Object[]") { FailTest("expected System.Object[] but was " + arrayType.FullName); } else { PassTest(); } var genericType = typeof(List <object>); StartTest("type of generic"); if (genericType.FullName != "System.Collections.Generic.List`1[[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]]") { FailTest("expected System.Collections.Generic.List`1[[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]] but was " + genericType.FullName); } else { PassTest(); } StartTest("Type GetFields length"); var x = new ClassForMetaTests(); var s = x.StringField; var i = x.IntField; var classForMetaTestsType = typeof(ClassForMetaTests); FieldInfo[] fields = classForMetaTestsType.GetFields(); EndTest(fields.Length == 3); StartTest("Type get string field via reflection"); var stringFieldInfo = classForMetaTestsType.GetField("StringField"); EndTest((string)stringFieldInfo.GetValue(x) == s); StartTest("Type get int field via reflection"); var intFieldInfo = classForMetaTestsType.GetField("IntField"); EndTest((int)intFieldInfo.GetValue(x) == i); StartTest("Type get static int field via reflection"); var staticIntFieldInfo = classForMetaTestsType.GetField("StaticIntField"); EndTest((int)staticIntFieldInfo.GetValue(x) == 23); StartTest("Type set string field via reflection"); stringFieldInfo.SetValue(x, "bcd"); EndTest(x.StringField == "bcd"); StartTest("Type set int field via reflection"); intFieldInfo.SetValue(x, 456); EndTest(x.IntField == 456); StartTest("Type set static int field via reflection"); staticIntFieldInfo.SetValue(x, 987); EndTest(ClassForMetaTests.StaticIntField == 987); var st = new StructForMetaTests(); st.StringField = "xyz"; var fieldStructType = typeof(StructForMetaTests); var structStringFieldInfo = fieldStructType.GetField("StringField"); StartTest("Struct get string field via reflection"); EndTest((string)structStringFieldInfo.GetValue(st) == "xyz"); StartTest("Class get+invoke ctor via reflection"); var ctor = classForMetaTestsType.GetConstructor(new Type[0]); ClassForMetaTests instance = (ClassForMetaTests)ctor.Invoke(null); EndTest(instance.IntField == 12); instance.ReturnTrueIf1(0); // force method output instance.ReturnTrueIf1AndThis(0, null); // force method output ClassForMetaTests.ReturnsParam(null); // force method output StartTest("Class get+invoke simple method via reflection"); var mtd = classForMetaTestsType.GetMethod("ReturnTrueIf1"); bool shouldBeTrue = (bool)mtd.Invoke(instance, new object[] { 1 }); bool shouldBeFalse = (bool)mtd.Invoke(instance, new object[] { 2 }); EndTest(shouldBeTrue && !shouldBeFalse); StartTest("Class get+invoke method with ref param via reflection"); var mtdWith2Params = classForMetaTestsType.GetMethod("ReturnTrueIf1AndThis"); shouldBeTrue = (bool)mtdWith2Params.Invoke(instance, new object[] { 1, instance }); shouldBeFalse = (bool)mtdWith2Params.Invoke(instance, new object[] { 1, new ClassForMetaTests() }); EndTest(shouldBeTrue && !shouldBeFalse); StartTest("Class get+invoke static method with ref param via reflection"); var staticMtd = classForMetaTestsType.GetMethod("ReturnsParam"); var retVal = (ClassForMetaTests)staticMtd.Invoke(null, new object[] { instance }); EndTest(Object.ReferenceEquals(retVal, instance)); }
private static void TestMetaData() { var typeGetType = Type.GetType("System.Char, System.Private.CoreLib"); if (typeGetType == null) { PrintLine("type == null. Simple class metadata test: Failed"); } else { if (typeGetType.FullName != "System.Char") { PrintLine("type != System.Char. Simple class metadata test: Failed"); } else { PrintLine("Simple class metadata test: Ok."); } } var typeofChar = typeof(Char); if (typeofChar == null) { PrintLine("type == null. Simple struct metadata test: Failed"); } else { if (typeofChar.FullName != "System.Char") { PrintLine("type != System.Char. Simple struct metadata test: Failed"); } else { PrintLine("Simple struct metadata test (typeof(Char)): Ok."); } } var gentT = new Gen <int>(); var genParamType = gentT.TestTypeOf(); PrintString("type of generic parameter: "); if (genParamType.FullName != "System.Int32") { PrintString("expected System.Int32 but was " + genParamType.FullName); PrintLine(" Failed."); } else { PrintLine("Ok."); } var arrayType = typeof(object[]); PrintString("type of array: "); if (arrayType.FullName != "System.Object[]") { PrintString("expected System.Object[] but was " + arrayType.FullName); PrintLine(" Failed."); } else { PrintLine("Ok."); } var genericType = typeof(List <object>); PrintString("type of generic : "); if (genericType.FullName != "System.Collections.Generic.List`1[[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]]") { PrintString("expected System.Collections.Generic.List`1[[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]] but was " + genericType.FullName); PrintLine(" Failed."); } else { PrintLine("Ok."); } PrintString("Type GetFields length: "); var x = new ClassForMetaTests(); var s = x.StringField; var i = x.IntField; var fieldClassType = typeof(ClassForMetaTests); FieldInfo[] fields = fieldClassType.GetFields(); if (fields.Length == 3) { PrintLine("Ok."); } else { PrintLine(" Failed."); } PrintString("Type get string field via reflection: "); var stringFieldInfo = fieldClassType.GetField("StringField"); if ((string)stringFieldInfo.GetValue(x) == s) { PrintLine("Ok."); } else { PrintLine("Failed."); } PrintString("Type get int field via reflection: "); var intFieldInfo = fieldClassType.GetField("IntField"); if ((int)intFieldInfo.GetValue(x) == i) { PrintLine("Ok."); } else { PrintLine("Failed."); } PrintString("Type get static int field via reflection: "); var staticIntFieldInfo = fieldClassType.GetField("StaticIntField"); if ((int)staticIntFieldInfo.GetValue(x) == 23) { PrintLine("Ok."); } else { PrintLine("Failed."); } PrintString("Type set string field via reflection: "); stringFieldInfo.SetValue(x, "bcd"); if (x.StringField == "bcd") { PrintLine("Ok."); } else { PrintLine("Failed."); } PrintString("Type set int field via reflection: "); intFieldInfo.SetValue(x, 456); if (x.IntField == 456) { PrintLine("Ok."); } else { PrintLine("Failed."); } PrintString("Type set static int field via reflection: "); staticIntFieldInfo.SetValue(x, 987); if (ClassForMetaTests.StaticIntField == 987) { PrintLine("Ok."); } else { PrintLine("Failed."); } var st = new StructForMetaTests(); st.StringField = "xyz"; var fieldStructType = typeof(StructForMetaTests); var structStringFieldInfo = fieldStructType.GetField("StringField"); PrintString("Struct get string field via reflection: "); if ((string)structStringFieldInfo.GetValue(st) == "xyz") { PrintLine("Ok."); } else { PrintLine("Failed."); } }