VisionToolFactory.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using CommonMethods;
  9. using System.IO;
  10. namespace VisionJobFactory
  11. {
  12. public class VisionToolFactory
  13. {
  14. private static Dictionary<ToolType, Type> animalTypeDic = new Dictionary<ToolType, Type>();
  15. public static void InitVisionToolTypeDic()
  16. {
  17. animalTypeDic.Clear();
  18. //读取所有带有VisionToolAttribute的类
  19. var classEnumerator = new ClassEnumerator(typeof(VisionToolAttribute), null, typeof(VisionToolAttribute).Assembly);
  20. var em = classEnumerator.Results.GetEnumerator();
  21. while (em.MoveNext())
  22. {
  23. var classType = em.Current;
  24. var atts = classType.GetCustomAttributes(typeof(VisionToolAttribute), true);
  25. if (atts.Length > 0)
  26. {
  27. var att = atts[0] as VisionToolAttribute;
  28. if (null != att)
  29. {
  30. animalTypeDic.Add(att.ToolType, classType);
  31. }
  32. }
  33. }
  34. }
  35. public static IToolInfo CreateToolVision(ToolType animalType, string toolName)
  36. {
  37. if (animalTypeDic.ContainsKey(animalType))
  38. {
  39. return (IToolInfo)Activator.CreateInstance(animalTypeDic[animalType], new object[] { toolName });
  40. }
  41. return null;
  42. }
  43. public static IToolInfo CreateToolVision(ToolType animalType)
  44. {
  45. if (animalTypeDic.ContainsKey(animalType))
  46. {
  47. return (IToolInfo)Activator.CreateInstance(animalTypeDic[animalType]);
  48. }
  49. return null;
  50. }
  51. }
  52. //根据Attribute提取类
  53. public class ClassEnumerator
  54. {
  55. protected List<Type> results = new List<Type>();
  56. public List<Type> Results
  57. {
  58. get
  59. {
  60. return results;
  61. }
  62. }
  63. private Type AttributeType;
  64. private Type InterfaceType;
  65. public ClassEnumerator(Type InAttributeType, Type InInterfaceType, Assembly InAssembly, bool bIgnoreAbstract = true, bool bInheritAttribute = false, bool bShouldCrossAssembly = true)
  66. {
  67. AttributeType = InAttributeType;
  68. InterfaceType = InInterfaceType;
  69. try
  70. {
  71. if (bShouldCrossAssembly)
  72. {
  73. Assembly[] Assemblys = AppDomain.CurrentDomain.GetAssemblies();
  74. if (Assemblys != null)
  75. {
  76. for (int i = 0, len = Assemblys.Length; i < len; i++)
  77. {
  78. CheckInAssembly(Assemblys[i], bIgnoreAbstract, bInheritAttribute);
  79. }
  80. }
  81. //List<Assembly> allAssemblies = new List<Assembly>();
  82. //string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  83. //string[] FILES = Directory.GetFiles(path, "*.dll");
  84. //foreach (string dll in Directory.GetFiles(path, "*.dll"))
  85. // allAssemblies.Add(Assembly.LoadFile(dll));
  86. // 手动寻找该dll,若dll名称改变需要重新编译,不智能
  87. // Assembly assem = Assembly.LoadFile($"{AppDomain.CurrentDomain.BaseDirectory}ToolLib.VisionToolList.dll");
  88. //if (allAssemblies != null)
  89. //{
  90. // for (int i = 0; i < allAssemblies.Count; i++)
  91. // {
  92. // CheckInAssembly(allAssemblies[i], bIgnoreAbstract, bInheritAttribute);
  93. // }
  94. //}
  95. }
  96. else
  97. {
  98. CheckInAssembly(InAssembly, bIgnoreAbstract, bInheritAttribute);
  99. }
  100. }
  101. catch (Exception e)
  102. {
  103. Debug.WriteLine("Error in enumerate classes: " + e.Message);
  104. }
  105. }
  106. private void CheckInAssembly(Assembly InAssembly, bool bInIgnoreAbstract, bool bInInheritAttribute)
  107. {
  108. Type[] types = InAssembly.GetTypes();
  109. if (null == types)
  110. {
  111. return;
  112. }
  113. for (int i = 0, len = types.Length; i < len; i++)
  114. {
  115. var type = types[i];
  116. if (InterfaceType == null || InterfaceType.IsAssignableFrom(type))
  117. {
  118. if (!bInIgnoreAbstract || (bInIgnoreAbstract && !type.IsAbstract))
  119. {
  120. if (type.GetCustomAttributes(AttributeType, bInInheritAttribute).Length > 0)
  121. {
  122. results.Add(type);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }