AssemblyHelper.cs 3.7 KB

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