代码要恰如其分,说的是只实现当前需要的功能,保留一定的可扩展性,但不要为了在很长一段时间内都不会发生的事情实现可扩展性。
不要预测将来,将来的新需求可以通过不断的重构来保持代码的健康和可扩展性。

下面是一个在WCF内部服务中实现的类,这个前提很重要,这是一个服务的内部实现,不是契约,是不需要对外发布的组件(不含服务契约,契约通过另外的组件发布)。

 1 public static class PictureRecognizerFactory
 2     {
 3         private static PictureRecognizer[] m_Recognizers;
 4         private static object _oSync = new object();
 5 
 6         private static PictureRecognizer[] m_AllRecognizers
 7         {
 8             get
 9             {
10                 if (m_Recognizers == null)
11                 {
12                     lock (_oSync)
13                     {
14                         if (m_Recognizers == null)
15                         {
16                             m_Recognizers = FindAllPictureRecognizer();
17                         }
18                     }
19                 }
20                 return m_Recognizers;
21             }
22         }
23 
24         /// <summary>
25         /// 获取图片的类别
26         /// </summary>
27         /// <param name="picture"></param>
28         /// <returns></returns>
29         public static PictureType GetPictureType(Picture picture)
30         {
31             foreach (PictureRecognizer item in m_AllRecognizers)
32             {
33                 PictureType type = item.Reconize(picture);
34                 if (type != PictureType.Unknown)
35                     return type;
36             }
37             return PictureType.Unknown;
38 
39         }
40 
41         /// <summary>
42         /// 寻找所有被标识了PictureRecognizerAttribute的类
43         /// </summary>
44         /// <returns></returns>
45         private static PictureRecognizer[] FindAllPictureRecognizer()
46         {
47             Type[] typesWithRecognizer = GetTypesWithRecognizers();
48 
49             List<PictureRecognizer> list = new List<PictureRecognizer>();
50             foreach (Type type in typesWithRecognizer)
51             {
52                 list.Add((PictureRecognizer)type.Assembly.CreateInstance(type.FullName));
53             }
54 
55             return list.ToArray();
56         }
57 
58         /// <summary>
59         /// 获取所有标记为RecognizerAttribute的类型
60         /// </summary>
61         /// <returns></returns>
62         private static Type[] GetTypesWithRecognizers()
63         {
64             List<Type> typesRecognizers = new List<Type>();
65             Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
66             foreach (Assembly ass in assemblies)
67             {
68                 try
69                 {
70                     Type[] types = ass.GetTypes();
71                     foreach (Type type in types)
72                     {
73                         if (type.GetCustomAttributes(typeof(PictureRecognizerAttribute), true).Length > 0)
74                         {
75                             typesRecognizers.Add(type);
76                         }
77 
78                     }
79                 }
80                 catch { }//如果在某个Assembly中加载不到类型,则不予理会,不处理异常
81             }
82             if (typesRecognizers.Count == 0)
83                 throw new Exception("找不到任何图像识别器");
84             return typesRecognizers.ToArray();
85         }
86     }
View Code

相关文章:

  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
  • 2020-10-28
  • 2021-11-17
  • 2021-12-02
  • 2021-10-06
猜你喜欢
  • 2021-09-18
  • 2021-07-01
相关资源
相似解决方案