VisionToolFactory.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. namespace VisionJobFactory
  10. {
  11. public class VisionToolFactory
  12. {
  13. private static Dictionary<ToolType, Type> animalTypeDic = new Dictionary<ToolType, Type>();
  14. public static void InitVisionToolTypeDic()
  15. {
  16. animalTypeDic.Clear();
  17. //读取所有带有VisionToolAttribute的类
  18. var classEnumerator = new ClassEnumerator(typeof(VisionToolAttribute), null, typeof(VisionToolAttribute).Assembly);
  19. var em = classEnumerator.Results.GetEnumerator();
  20. while (em.MoveNext())
  21. {
  22. var classType = em.Current;
  23. var atts = classType.GetCustomAttributes(typeof(VisionToolAttribute), true);
  24. if (atts.Length > 0)
  25. {
  26. var att = atts[0] as VisionToolAttribute;
  27. if (null != att)
  28. {
  29. animalTypeDic.Add(att.ToolType, classType);
  30. }
  31. }
  32. }
  33. }
  34. public static IToolInfo CreateToolVision(ToolType animalType, string toolName)
  35. {
  36. if (animalTypeDic.ContainsKey(animalType))
  37. {
  38. return (IToolInfo)Activator.CreateInstance(animalTypeDic[animalType], new object[] { toolName });
  39. }
  40. return null;
  41. }
  42. public static IToolInfo CreateToolVision(ToolType animalType)
  43. {
  44. if (animalTypeDic.ContainsKey(animalType))
  45. {
  46. return (IToolInfo)Activator.CreateInstance(animalTypeDic[animalType]);
  47. }
  48. return null;
  49. }
  50. }
  51. //根据Attribute提取类
  52. public class ClassEnumerator
  53. {
  54. protected List<Type> results = new List<Type>();
  55. public List<Type> Results
  56. {
  57. get
  58. {
  59. return results;
  60. }
  61. }
  62. private Type AttributeType;
  63. private Type InterfaceType;
  64. public ClassEnumerator(Type InAttributeType, Type InInterfaceType, Assembly InAssembly, bool bIgnoreAbstract = true, bool bInheritAttribute = false, bool bShouldCrossAssembly = false)
  65. {
  66. AttributeType = InAttributeType;
  67. InterfaceType = InInterfaceType;
  68. try
  69. {
  70. if (bShouldCrossAssembly)
  71. {
  72. Assembly[] Assemblys = AppDomain.CurrentDomain.GetAssemblies();
  73. if (Assemblys != null)
  74. {
  75. for (int i = 0, len = Assemblys.Length; i < len; i++)
  76. {
  77. CheckInAssembly(Assemblys[i], bIgnoreAbstract, bInheritAttribute);
  78. }
  79. }
  80. }
  81. else
  82. {
  83. CheckInAssembly(InAssembly, bIgnoreAbstract, bInheritAttribute);
  84. }
  85. }
  86. catch (Exception e)
  87. {
  88. Debug.WriteLine("Error in enumerate classes: " + e.Message);
  89. }
  90. }
  91. private void CheckInAssembly(Assembly InAssembly, bool bInIgnoreAbstract, bool bInInheritAttribute)
  92. {
  93. Type[] types = InAssembly.GetTypes();
  94. if (null == types)
  95. {
  96. return;
  97. }
  98. for (int i = 0, len = types.Length; i < len; i++)
  99. {
  100. var type = types[i];
  101. if (InterfaceType == null || InterfaceType.IsAssignableFrom(type))
  102. {
  103. if (!bInIgnoreAbstract || (bInIgnoreAbstract && !type.IsAbstract))
  104. {
  105. if (type.GetCustomAttributes(AttributeType, bInInheritAttribute).Length > 0)
  106. {
  107. results.Add(type);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }