Reflection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Reflection;
  5. using System.IO;
  6. using CommonLang.Log;
  7. namespace CommonLang.Property
  8. {
  9. public class PropertyList<T> : List<T> where T : new()
  10. {
  11. public PropertyList()
  12. {
  13. }
  14. public PropertyList(T[] other)
  15. {
  16. AddRange(other);
  17. }
  18. public PropertyList(ICollection<T> other)
  19. {
  20. AddRange(other);
  21. }
  22. }
  23. public class TypeComparer : IComparer<Type>
  24. {
  25. public int Compare(Type x, Type y)
  26. {
  27. return x.FullName.CompareTo(y.FullName);
  28. }
  29. }
  30. public class ToStringComparer<T> : IComparer<T>
  31. {
  32. public int Compare(T x, T y)
  33. {
  34. return x.ToString().CompareTo(y.ToString());
  35. }
  36. }
  37. public static class ReflectionUtil
  38. {
  39. public static void LoadDlls(DirectoryInfo dirinfo)
  40. {
  41. AppDomain domain = AppDomain.CurrentDomain;
  42. foreach (FileInfo file in dirinfo.GetFiles())
  43. {
  44. if (file.Extension.ToLower().Equals(".dll"))
  45. {
  46. try
  47. {
  48. Assembly asm = Assembly.ReflectionOnlyLoadFrom(file.FullName);
  49. if (!ExistAssembly(domain, asm.FullName))
  50. {
  51. domain.Load(asm.FullName);
  52. }
  53. }
  54. catch (Exception err)
  55. {
  56. Console.WriteLine(err.Message);
  57. }
  58. }
  59. }
  60. }
  61. private static bool ExistAssembly(AppDomain domain, string name)
  62. {
  63. Assembly[] asms = domain.GetAssemblies();
  64. foreach (Assembly asm in asms)
  65. {
  66. if (asm.FullName.Equals(name))
  67. {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. public static Type LoadTypeFromDLL<T>(string path)
  74. {
  75. Assembly asm = Assembly.ReflectionOnlyLoadFrom(path);
  76. asm = AppDomain.CurrentDomain.Load(asm.FullName);
  77. Type data_plugin_type = ReflectionUtil.FindTypeFromAssembly(asm, typeof(T));
  78. return data_plugin_type;
  79. }
  80. public static T LoadInterfaceFromDLL<T>(string path)
  81. {
  82. Assembly asm = Assembly.ReflectionOnlyLoadFrom(path);
  83. asm = AppDomain.CurrentDomain.Load(asm.FullName);
  84. Type data_plugin_type = ReflectionUtil.FindTypeFromAssembly(asm, typeof(T));
  85. T formula = (T)ReflectionUtil.CreateInstance(data_plugin_type);
  86. return formula;
  87. }
  88. public static T LoadInterfaceFromAssemblyName<T>(string assembly_name)
  89. {
  90. Assembly asm = AppDomain.CurrentDomain.Load(assembly_name);
  91. Type data_plugin_type = ReflectionUtil.FindTypeFromAssembly(asm, typeof(T));
  92. T formula = (T)ReflectionUtil.CreateInstance(data_plugin_type);
  93. return formula;
  94. }
  95. public static Type FindTypeFromAssembly(Assembly assembly, Type baseType)
  96. {
  97. foreach (Type type in assembly.GetTypes())
  98. {
  99. if (baseType.IsAssignableFrom(type))
  100. {
  101. return type;
  102. }
  103. }
  104. return null;
  105. }
  106. public static object CreateInstance(Type valueType, params object[] args)
  107. {
  108. try
  109. {
  110. if (valueType.IsArray)
  111. {
  112. int[] lengths = new int[valueType.GetArrayRank()];
  113. for (int i = 0; i < lengths.Length; i++)
  114. {
  115. if (i < args.Length)
  116. {
  117. lengths[i] = (int)args[i];
  118. }
  119. else
  120. {
  121. lengths[i] = 0;
  122. }
  123. }
  124. var elementType = valueType.GetElementType();
  125. return Array.CreateInstance(elementType, lengths);
  126. }
  127. else
  128. {
  129. return Activator.CreateInstance(valueType, args);
  130. }
  131. }
  132. catch (Exception err)
  133. {
  134. throw new Exception(valueType.FullName, err);
  135. }
  136. }
  137. public static T CreateInterface<T>(string type, params object[] args)
  138. {
  139. Type data_plugin_type = ReflectionUtil.GetType(type);
  140. T formula = (T)ReflectionUtil.CreateInstance(data_plugin_type, args);
  141. return formula;
  142. }
  143. public static T CreateInterface<T>(Type data_plugin_type, params object[] args)
  144. {
  145. T formula = (T)ReflectionUtil.CreateInstance(data_plugin_type, args);
  146. return formula;
  147. }
  148. public static object CreateInstance(Type valueType)
  149. {
  150. return CreateInstance(valueType, new object[0]);
  151. }
  152. public static T CreateInterface<T>(string type)
  153. {
  154. return CreateInterface<T>(type, new object[0]);
  155. }
  156. public static T CreateInterface<T>(Type data_plugin_type)
  157. {
  158. return CreateInterface<T>(data_plugin_type, new object[0]);
  159. }
  160. public static Type GetType(string vtype)
  161. {
  162. Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
  163. foreach (Assembly asm in asms)
  164. {
  165. try
  166. {
  167. Type ret = asm.GetType(vtype, false, true);
  168. if (ret != null)
  169. {
  170. return ret;
  171. }
  172. }
  173. catch (Exception err)
  174. {
  175. Console.Write(ReflectionUtil.exception2String(err));
  176. }
  177. }
  178. return null;
  179. }
  180. public static List<Type> GetSubTypes(Type superType)
  181. {
  182. HashMap<string, Type> map = new HashMap<string, Type>();
  183. foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
  184. {
  185. try
  186. {
  187. Type[] types = asm.GetTypes();
  188. foreach (Type sub in types)
  189. {
  190. if (sub.IsClass && superType.IsAssignableFrom(sub))
  191. {
  192. map.Put(sub.FullName, sub);
  193. }
  194. }
  195. }
  196. catch (Exception err)
  197. {
  198. Console.Write(ReflectionUtil.exception2String(err));
  199. }
  200. }
  201. List<Type> ret = new List<Type>(map.Values);
  202. ret.Sort(new TypeComparer());
  203. return ret;
  204. }
  205. /// <summary>
  206. /// 获取所有匹配的非虚子类
  207. /// </summary>
  208. /// <param name="superType"></param>
  209. /// <returns></returns>
  210. public static List<Type> GetNoneVirtualSubTypes(Type superType)
  211. {
  212. HashMap<string, Type> map = new HashMap<string, Type>();
  213. Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
  214. foreach (Assembly asm in asms)
  215. {
  216. try
  217. {
  218. Type[] types = asm.GetTypes();
  219. foreach (Type sub in types)
  220. {
  221. if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub))
  222. {
  223. map.Put(sub.FullName, sub);
  224. }
  225. }
  226. }
  227. catch (Exception err)
  228. {
  229. Console.Write(ReflectionUtil.exception2String(err));
  230. }
  231. }
  232. List<Type> ret = new List<Type>(map.Values);
  233. ret.Sort(new TypeComparer());
  234. return ret;
  235. }
  236. static string exception2String(Exception e)
  237. {
  238. if (e is ReflectionTypeLoadException)
  239. {
  240. var sb = new StringBuilder();
  241. foreach (var le in (e as ReflectionTypeLoadException).LoaderExceptions)
  242. {
  243. sb.Append(le.Message).Append("\r\n");
  244. }
  245. return sb.ToString();
  246. }
  247. return e.ToString();
  248. }
  249. public static List<Type> GetNoneVirtualSubTypes(Type superType, Assembly asm)
  250. {
  251. HashMap<string, Type> map = new HashMap<string, Type>();
  252. Type[] types = asm.GetTypes();
  253. foreach (Type sub in types)
  254. {
  255. if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub))
  256. {
  257. map.Put(sub.FullName, sub);
  258. }
  259. }
  260. List<Type> ret = new List<Type>(map.Values);
  261. ret.Sort(new TypeComparer());
  262. return ret;
  263. }
  264. /// <summary>
  265. /// 获取所有匹配的非虚子类,每个类必须标注DescAttribute
  266. /// </summary>
  267. /// <param name="superType"></param>
  268. /// <returns></returns>
  269. public static List<TypeDescAttribute> GetNoneVirtualSubDescTypes(Type superType)
  270. {
  271. HashMap<string, TypeDescAttribute> map = new HashMap<string, TypeDescAttribute>();
  272. Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
  273. foreach (Assembly asm in asms)
  274. {
  275. try
  276. {
  277. Type[] types = asm.GetTypes();
  278. foreach (Type sub in types)
  279. {
  280. if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub))
  281. {
  282. DescAttribute attr = PropertyUtil.GetAttribute<DescAttribute>(sub);
  283. if (attr != null)
  284. {
  285. map.Put(sub.FullName, new TypeDescAttribute(sub));
  286. }
  287. }
  288. }
  289. }
  290. catch (Exception err)
  291. {
  292. Console.Write(ReflectionUtil.exception2String(err));
  293. }
  294. }
  295. List<TypeDescAttribute> ret = new List<TypeDescAttribute>(map.Values);
  296. ret.Sort(new ToStringComparer<TypeDescAttribute>());
  297. return ret;
  298. }
  299. public static List<TypeDescAttribute> GetNoneVirtualSubDescTypes(Type superType, Assembly asm)
  300. {
  301. HashMap<string, TypeDescAttribute> map = new HashMap<string, TypeDescAttribute>();
  302. Type[] types = asm.GetTypes();
  303. foreach (Type sub in types)
  304. {
  305. if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub))
  306. {
  307. DescAttribute attr = PropertyUtil.GetAttribute<DescAttribute>(sub);
  308. if (attr != null)
  309. {
  310. map.Put(sub.FullName, new TypeDescAttribute(sub));
  311. }
  312. }
  313. }
  314. List<TypeDescAttribute> ret = new List<TypeDescAttribute>(map.Values);
  315. ret.Sort(new ToStringComparer<TypeDescAttribute>());
  316. return ret;
  317. }
  318. }
  319. }