在制作一个批量序列化工具时遇到了如下问题,在此记录一下,仅供参考。
主程序加载另一个程序集,将其中的所有类取出,然后对这些类分别调用泛型类或泛型方法。控制台程序解决方案如下:
Main工程的Program的Main方法中,一般情况下,调用Worker的泛型方法来处理测试类的话,可以写为:
Worker worker = new Worker();
worker.DoWork<Apple>();
worker.DoWork<Cat>();
worker.DoWork<Person>();
但是,如果MockClassLib中需要处理的类型非常多时,这样显示调用必然是不灵活的,应当怎样向泛型方法DoWork<T>()的尖括号中动态传入类型呢?
考虑代码:
//Load assembly Assembly mockAssembly = Assembly.LoadFrom("MockClassLibrary.dll"); Type[] typeArray = mockAssembly.GetTypes();
//Create instance of Worker Worker worker = new Worker(); foreach(Type curType in typeArray) { worker.DoWork<curType>(); //Error }
可以看到,Type类型的实例是无法直接传入泛型方法的尖括号中的,T要求显式指明类型名。
下面通过反射方式来获取泛型方法,并创建特定类型的泛型方法。
-
对于非静态方法:public void DoWork<T>()
对于非静态方法,调用MethodInfo.Invoke(object, object[])时,第一个参数需要指明泛型方法的所有者(即这里创建的worker对象),第二个参数为泛
型方法的参数列表,DoWork<T>()没有输入参数,所以设为null
//Create an instance of Worker Worker worker = new Worker();
//Get type of Worker Type workerType = typeof(Worker);
//Get Generic Method MethodInfo doWorkMethod = workerType.GetMethod("DoWork");
//Invoke DoWork<T> with different Type foreach (Type curType in typeArray) { if (curType.IsClass && !curType.IsAbstract)//Filter BaseEntity { MethodInfo curMethod = doWorkMethod.MakeGenericMethod(curType); curMethod.Invoke(worker, null);//Member method,use instance } }
-
对于静态方法:public static void StaticDoWork<T>()
不同于非静态方法,这里直接反射的类静态方法,所以Invoke()的第一个参数设为null
//Get type of Worker Worker worker = new Worker();
//Get Generic Method MethodInfo staticDoWorkMethod = workerType.GetMethod("StaticDoWork");
//Invoke StaticDoWork<T> foreach (Type curType in typeArray) { if (curType.IsClass && !curType.IsAbstract) { MethodInfo curMethod = staticDoWorkMethod.MakeGenericMethod(curType); curMethod.Invoke(null, null);//Static method } }
-
对于有返回值的非静态方法:public List<T> GetList()
如同动态调用DoWork<T>()方法一样,只是在处理返回值时,可以使用下面的方法
1 IList tempList = (IList)curMethod.Invoke(worker, null); 2 //Or 3 IEnumerable tempList = (IEnumerable)curMethod.Invoke(worker, null);
下面要使用泛型类XMLTool<T>的静态方法public static void XmlSerialize_Save(List<T> list, string dirPath, string fileName)方法。
首先应通过反射构造出指定类型的泛型类XMLTool<T>,再反射出其中的XmlSerialize_Save方法并使用。
1 //Use Generic Class 2 Type xmlToolType = typeof(XMLTool<>).MakeGenericType(curType); 3 4 //Get method 5 MethodInfo saveMethod = xmlToolType.GetMethod("XmlSerialize_Save"); 6 7 //Invoke 8 saveMethod.Invoke 9 ( 10 null, //Static method 11 new object[] { resultList, @"c:\", @"c:\Test_" + curType.Name + ".xml" }
12 );
Program-->Main()方法的全部代码:
1 namespace RetrieveUnknownClass 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 //Load assembly 8 Assembly mockAssembly = Assembly.LoadFrom("MockClassLibrary.dll"); 9 Type[] typeArray = mockAssembly.GetTypes(); 10 11 //Create instance of Worker 12 Type workerType = typeof(Worker); 13 Worker worker = new Worker(); 14 15 #region Member method 16 17 Console.WriteLine(">>>>>>>>>Use Generic Method:"); 18 MethodInfo doWorkMethod = workerType.GetMethod("DoWork"); 19 20 //Invoke DoWork<T> 21 foreach (Type curType in typeArray) 22 { 23 if (curType.IsClass && !curType.IsAbstract) 24 { 25 MethodInfo curMethod = doWorkMethod.MakeGenericMethod(curType); 26 curMethod.Invoke(worker, null);//Member method,use instance 27 } 28 } 29 30 #endregion 31 32 #region Static method 33 34 Console.WriteLine("\r\n>>>>>>>>>Use Static Generic Method:"); 35 MethodInfo staticDoWorkMethod = workerType.GetMethod("StaticDoWork"); 36 37 //Invoke StaticDoWork<T> 38 foreach (Type curType in typeArray) 39 { 40 if (curType.IsClass && !curType.IsAbstract) 41 { 42 MethodInfo curMethod = staticDoWorkMethod.MakeGenericMethod(curType); 43 curMethod.Invoke(null, null);//Static method 44 } 45 } 46 47 #endregion 48 49 #region Get A List & Serialize It to Xml File With Generic 50 51 Console.WriteLine("\r\n>>>>>>>>>Get List By Generic Method:"); 52 MethodInfo getListMethod = workerType.GetMethod("GetList"); 53 54 foreach (Type curType in typeArray) 55 { 56 if (curType.IsClass && !curType.IsAbstract) 57 { 58 MethodInfo curMethod = getListMethod.MakeGenericMethod(curType); 59 //Generate List 60 IList resultList = (IList)curMethod.Invoke(worker, null); 61 //Show List 62 ShowList(resultList); 63 //Use Generic Class 64 Type xmlToolType = typeof(XMLTool<>).MakeGenericType(curType); 65 MethodInfo saveMethod = xmlToolType.GetMethod("XmlSerialize_Save"); 66 67 saveMethod.Invoke 68 ( 69 null, //Static method 70 new object[] { resultList, @"c:\", @"c:\Test_" + curType.Name + ".xml" } 71 ); 72 } 73 } 74 75 Console.WriteLine("Serialization Completed...\r\n"); 76 #endregion 77 } 78 79 public static void ShowList(IList list) 80 { 81 Console.WriteLine("Type of list: {0}\r\nCount of current list: {1}\r\nType of item in list: {2}\r\n", 82 list.GetType(), 83 list.Count, 84 list[0].GetType()); 85 } 86 } 87 }
|
请发表评论