public void FieldValueTest() { Type o; object fromObj; TypeProcessor processor = new TypeProcessor(typeof(TestType)); fromObj = processor.CreateInstance(); processor.SetField(fromObj, "A", new TestType()); processor.SetField(fromObj, "B", new TestType()); processor.SetField(fromObj, "C", new TestType()); processor.SetField(fromObj, "D", new TestType()); processor.SetField(fromObj, "E", new TestType()); processor.SetField(fromObj, "F", new TestType()); processor.SetField(fromObj, "G", new TestType()); processor.SetField(fromObj, "H", new TestType()); processor.SetField(fromObj, "I", "I"); processor.SetField(fromObj, "J", 99); processor.SetField(fromObj, "K", true); for (int i = 0; i < 10; i++) { var ToObj = processor.CreateInstance(); foreach (FieldInfo item in fromObj.GetType().GetFields()) { processor.SetField(ToObj, item.Name, processor.GetField(fromObj, item.Name)); if (!processor.GetField(fromObj, item.Name).Equals(processor.GetField(ToObj, item.Name))) { Assert.Fail(); } } } }
public void PropertyValueTest() { Type o; object fromObj; TypeProcessor processor = new TypeProcessor(typeof(TestType)); fromObj = processor.CreateInstance(); processor.SetProperty(fromObj, "L", new TestType()); processor.SetProperty(fromObj, "M", new TestType()); processor.SetProperty(fromObj, "N", new TestType()); processor.SetProperty(fromObj, "O", new TestType()); processor.SetProperty(fromObj, "P", new TestType()); processor.SetProperty(fromObj, "Q", new TestType()); processor.SetProperty(fromObj, "R", new TestType()); processor.SetProperty(fromObj, "S", new TestType()); processor.SetProperty(fromObj, "T", "I"); processor.SetProperty(fromObj, "U", 99); processor.SetProperty(fromObj, "V", true); processor.SetProperty(fromObj, "GenericTypeName", "A"); for (int i = 0; i < 10; i++) { var ToObj = processor.CreateInstance(); foreach (PropertyInfo item in fromObj.GetType().GetProperties()) { processor.SetProperty(ToObj, item.Name, processor.GetProperty(fromObj, item.Name)); if (!processor.GetProperty(fromObj, item.Name).Equals(processor.GetProperty(ToObj, item.Name))) { Assert.Fail(); } } } }
private void CreateInstance(Type type, Type[] genericTypes, string genericTypeName, Type[] argsType, params object[] args) { object instance; string compareResult = ""; TypeProcessor processor = new TypeProcessor(type, genericTypes, argsType); if (args != null) { foreach (var item in args) { compareResult += item.ToString(); } } instance = processor.CreateInstance(args); if (instance == null || instance.GetType().Name != type.Name) { Assert.Fail(); } else { string reslut = (string)processor.GetProperty(instance, "T") + (((int)processor.GetProperty(instance, "U")).ToString() == "0" ? "" : ((int)processor.GetProperty(instance, "U")).ToString()); if (reslut != compareResult || (string)processor.GetProperty(instance, "GenericTypeName") != genericTypeName) { Assert.Fail(); } } }
private void GenericMethodCallWithGenericType(Type type, string methodName, Type[] genericsType, Type[] parametersType, params object[] args) { TypeProcessor processor = new TypeProcessor(type, null, null); object instance = processor.CreateInstance(); string result; string resultCompare = type.GetGenericArguments()[0].Name + genericsType[0].Name; result = (string)processor.MethodCall(instance, methodName, genericsType, parametersType, args); if (args != null) { foreach (var item in args) { resultCompare += ":" + (string)item; } } if (result != resultCompare) { Assert.Fail(); } }
private void VoidCallWithGenericType(Type type, string methodName, Type[] genericsType, Type[] parametersType, params object[] args) { TypeProcessor processor = new TypeProcessor(type, null, null); object instance = processor.CreateInstance(); string resultCompare = genericsType[0].Name; processor.VoidCall(instance, methodName, genericsType, parametersType, args); if (args != null) { foreach (var item in args) { resultCompare += (string)item; } } string reslut = (string)processor.GetProperty(instance, "GenericTypeName") + (string)processor.GetField(instance, "I"); if (reslut != resultCompare) { Assert.Fail(); } }
static void Main(string[] args) { TimeSpan ts; Stopwatch stopWatch; TypeProcessor processor = new TypeProcessor(typeof(TestType)); object instance; instance = processor.CreateInstance(); processor.SetProperty(instance, "T", "dd"); for (int i = 0; i < 50000; i++) { instance = processor.CreateInstance(); } object fromObj; fromObj = processor.CreateInstance(); processor.SetProperty(fromObj, "L", new TestType()); processor.SetProperty(fromObj, "M", new TestType()); processor.SetProperty(fromObj, "N", new TestType()); processor.SetProperty(fromObj, "O", new TestType()); processor.SetProperty(fromObj, "P", new TestType()); processor.SetProperty(fromObj, "Q", new TestType()); processor.SetProperty(fromObj, "R", new TestType()); processor.SetProperty(fromObj, "S", new TestType()); processor.SetProperty(fromObj, "T", "I"); processor.SetProperty(fromObj, "U", 99); processor.SetProperty(fromObj, "V", true); stopWatch = new Stopwatch(); stopWatch.Start(); for (int i = 0; i < 50000; i++) { var ToObj = processor.CreateInstance(); foreach (PropertyInfo item in fromObj.GetType().GetProperties()) { processor.SetProperty(ToObj, item.Name, processor.GetProperty(fromObj, item.Name)); } } stopWatch.Stop(); ts = stopWatch.Elapsed; Console.WriteLine("This is a inject property value test"); Console.WriteLine("from a class has 11 property, and 8 complex properties and 3 basic properties"); Console.WriteLine("below is the test result..."); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("stand call 50000 times"); Console.WriteLine("RunTime: " + ts.ToString() + "ms"); stopWatch = new Stopwatch(); stopWatch.Start(); Type o = typeof(TestType); Core.delgCreateInstance delgCreate = Core.GenCreateInstanceDelg(Core.MakeCtorInfo(typeof(TestType), null)); List <Core.delgSetProperty> setters = new List <Core.delgSetProperty>(); List <Core.delgGetProperty> getters = new List <Core.delgGetProperty>(); PropertyInfo[] propertyInfos = fromObj.GetType().GetProperties(); for (int i = 0; i < propertyInfos.Count(); i++) { setters.Add(Core.GenSetPropertyValueDelg(o, propertyInfos[i])); getters.Add(Core.GenGetPropertyValueDelg(o, propertyInfos[i])); } object toObj; for (int i = 0; i < 1000000; i++) { toObj = delgCreate(); for (int n = 0; n < setters.Count(); n++) { setters[n](toObj, getters[n](fromObj)); } } stopWatch.Stop(); ts = stopWatch.Elapsed; Console.WriteLine(""); Console.WriteLine("delegate call 10000000 times"); Console.WriteLine("RunTime: " + ts.ToString() + "ms"); stopWatch = new Stopwatch(); stopWatch.Start(); PropertyInfo pp = fromObj.GetType().GetProperty("U"); Core.delgGetProperty dd = Core.GenGetPropertyValueDelg(o, pp); Core.delgSetProperty xx = Core.GenSetPropertyValueDelg(o, pp); for (int i = 0; i < 1000000; i++) { fromObj = delgCreate(); dd(fromObj); } stopWatch.Stop(); ts = stopWatch.Elapsed; Console.WriteLine(""); Console.WriteLine("delegate call 10000000 times"); Console.WriteLine("RunTime: " + ts.ToString() + "ms"); Console.ReadLine(); }