领胜LDS 键盘AOI检测项目
wells.liu
2020-07-06 f9c7928bff92596686e05a15fef21d499e954088
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Bro.Common.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
 
namespace Bro.Common.Factory
{
    public static class FactoryHelper
    {
        const string DLLPATTERN = "Bro.*.dll";
 
        private static List<Type> types = new List<Type>();
        public static List<Type> TYPES
        {
            get
            {
                if (types.Count <= 0)
                {
                    types = GetAttributeType<DeviceAttribute>().Values.ToList();
                }
 
                return types;
            }
        }
 
        public static Dictionary<T, Type> GetAttributeType<T>() where T : Attribute
        {
            Dictionary<T, Type> attrTypeDict = new Dictionary<T, Type>();
 
            var dllFiles = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).GetFiles(DLLPATTERN).Select(u => u.FullName).ToList();
 
            dllFiles.ForEach(f =>
            {
                try
                {
                    Assembly assm = Assembly.LoadFrom(f);
                    if (assm != null)
                    {
                        assm.GetTypes().ToList().ForEach(t =>
                        {
                            T attr = t.GetCustomAttribute<T>();
 
                            if (attr != null)
                            {
                                attrTypeDict[attr] = t;
                            }
                        });
                    }
                }
                catch (Exception)
                {
                }
            });
 
            return attrTypeDict;
        }
 
        //private static Dictionary<string, Type> device_dict = new Dictionary<string, Type>();
        //public static Dictionary<string, Type> DEVICE_DICT
        //{
        //    get
        //    {
        //        if (device_dict.Count < 0)
        //        {
 
        //        }
 
        //        return device_dict;
        //    }
        //}
 
        //private static List<string> device_type_names = new List<string>();
        //public static List<string> DEVICE_TYPE_NAMES
        //{
        //    get
        //    {
        //        if (device_type_names.Count < 0)
        //        {
 
        //        }
 
        //        return device_type_names;
        //    }
        //}
 
        public static Type GetTypeByAtrributeTypeName(string attrTypeName, EnumHelper.DeviceAttributeType attrType)
        {
            Type type = TYPES.FirstOrDefault(t =>
            {
                DeviceAttribute attr = t.GetCustomAttribute<DeviceAttribute>();
                return attr != null && attr.TypeCode == attrTypeName && attr.AttrType == attrType;
            });
 
            return type;
        }
    }
}