public void TestClassField() { ClassEntity obj = new ClassEntity(); Literacy lit = new Literacy(obj.GetType()); object value; //公共实例字段 lit.Load.PublicField(); lit.Field["ClassField"].SetValue(obj, "A"); value = lit.Field["ClassField"].GetValue(obj); Assert.AreEqual(value, "A"); lit.Field["StructField"].SetValue(obj, 1); value = lit.Field["StructField"].GetValue(obj); Assert.AreEqual(value, 1); //公共静态字段 lit.Load.StaticField(false); lit.Field["StaticClassField"].SetValue(obj, "B"); value = lit.Field["StaticClassField"].GetValue(obj); Assert.AreEqual(value, "B"); lit.Field["StaticStructField"].SetValue(obj, 2); value = lit.Field["StaticStructField"].GetValue(obj); Assert.AreEqual(value, 2); //私有实例字段 lit.Load.NonPublicField(); lit.Field["PrivateClassField"].SetValue(obj, "C"); value = lit.Field["PrivateClassField"].GetValue(obj); Assert.AreEqual(value, "C"); lit.Field["PrivateStructField"].SetValue(obj, 3); value = lit.Field["PrivateStructField"].GetValue(obj); Assert.AreEqual(value, 3); //私有静态字段 lit.Load.StaticField(true); lit.Field["PrivateStaticClassField"].SetValue(obj, "D"); value = lit.Field["PrivateStaticClassField"].GetValue(obj); Assert.AreEqual(value, "D"); lit.Field["PrivateStaticStructField"].SetValue(obj, 4); value = lit.Field["PrivateStaticStructField"].GetValue(obj); Assert.AreEqual(value, 4); }
public void TestClassMethod() { ClassEntity obj = new ClassEntity(); var methods = obj.GetType().GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static); foreach (var method in methods) { string str = "Z"; int i = 26; object[] args = new object[] { str, i }; var caller = Literacy.CreateCaller(method); if (method.Name.EndsWith("Ref")) { var val = caller(obj, args); obj.MethodRef(ref str, ref i); Assert.AreEqual(val, i); } else if (method.Name.EndsWith("Out")) { var val = caller(obj, args); obj.MethodOut(out str, out i); Assert.AreEqual(val, str); } else if (method.Name.EndsWith("Method")) { args = obj.Method(str, i); str = (string)args[0]; i = (int)args[1]; args = (object[])caller(obj, str, i); } else { continue; } Assert.AreEqual(args[0], str); Assert.AreEqual(args[1], i); if (method.Name.Contains("Static") == false) { Assert.AreEqual(obj.ClassProperty, str); Assert.AreEqual(obj.StructProperty, i); } } }