AssemblyHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections;
  3. using System.Reflection;
  4. using IwbZero.ToolCommon.StringModel;
  5. namespace IwbZero.ToolCommon.AssemblyHelpers
  6. {
  7. public static class AssemblyHelper
  8. {
  9. public static Type GetType(string typeName)
  10. {
  11. Type type = null;
  12. Assembly loadedAssembly = GetLoadedAssembly(typeName);
  13. if (loadedAssembly != null)
  14. type = loadedAssembly.GetType(typeName, false, true);
  15. return type;
  16. }
  17. public static Assembly GetLoadedAssembly(string typeName)
  18. {
  19. Assembly assembly1 = null;
  20. foreach (Assembly assembly2 in AppDomain.CurrentDomain.GetAssemblies())
  21. {
  22. if (assembly2.GetType(typeName) != null)
  23. {
  24. assembly1 = assembly2;
  25. break;
  26. }
  27. }
  28. return assembly1;
  29. }
  30. /// <summary>
  31. /// 创建实例
  32. /// </summary>
  33. /// <param name="typeName">类名</param>
  34. /// <param name="lib">程序集名</param>
  35. /// <returns></returns>
  36. public static object CreateInstance(string typeName, string lib)
  37. {
  38. object obj = null;
  39. if (typeName != "")
  40. {
  41. try
  42. {
  43. lib = lib.UAndT().Ew(".DLL");
  44. Assembly assembly = GetLoadedAssembly(typeName) ?? LoadAssembly(lib);
  45. if (assembly != null)
  46. obj = assembly.CreateInstance(typeName);
  47. }
  48. catch (Exception ex)
  49. {
  50. if (ex.InnerException != null) throw ex.InnerException;
  51. }
  52. }
  53. return obj;
  54. }
  55. /// <summary>
  56. /// 创建实例
  57. /// </summary>
  58. /// <param name="typeName">"程序集名"@"类名"</param>
  59. /// <returns></returns>
  60. public static object CreateInstance(string typeName)
  61. {
  62. object obj = null;
  63. Array arrayEx = typeName.StrToArray("@");
  64. if (arrayEx.Length >= 2)
  65. {
  66. string lib = arrayEx.GetValue(0) + ".DLL";
  67. obj = CreateInstance(arrayEx.GetValue(1)+"", lib);
  68. }
  69. return obj;
  70. }
  71. public static bool IsInterfaceOf(object obj, string @interface)
  72. {
  73. return obj.GetType().GetInterface(@interface, true) != null;
  74. }
  75. public static Assembly LoadAssembly(string lib)
  76. {
  77. Assembly assembly = null;
  78. string fullPathFileName = GetFullPathFileName(lib);
  79. if (fullPathFileName != "")
  80. {
  81. try
  82. {
  83. assembly = Assembly.LoadFrom(fullPathFileName);
  84. }
  85. catch (Exception ex)
  86. {
  87. string message = ex.Message;
  88. Console.WriteLine(message);
  89. }
  90. }
  91. return assembly;
  92. }
  93. public static ArrayList GetSubClassesOf(Type poBaseType, string libs)
  94. {
  95. ArrayList arrayList = new ArrayList();
  96. foreach (string lib in libs.StrToArray())
  97. {
  98. Assembly assembly = LoadAssembly(lib);
  99. if (assembly != null)
  100. {
  101. foreach (Type type in assembly.GetTypes())
  102. {
  103. bool flag = true;
  104. if (poBaseType != null)
  105. {
  106. if (poBaseType.IsInterface)
  107. {
  108. if (type.GetInterface(poBaseType.ToString(), true) == null)
  109. flag = false;
  110. }
  111. else if (!type.IsSubclassOf(poBaseType))
  112. flag = false;
  113. }
  114. if (flag)
  115. arrayList.Add(type.FullName);
  116. }
  117. }
  118. }
  119. return arrayList;
  120. }
  121. private static string GetFullPathFileName(string libs)
  122. {
  123. string str = $"{AppDomain.CurrentDomain.BaseDirectory}\\{libs}";
  124. return str;
  125. }
  126. }
  127. }