patrick
2019-12-10 1c4426810c71eead57084be8a18ade8d314dd8c4
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 Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace Bro.Common.Factory
{
    public static class FactoryHelper
    {
        private static List<Type> types = new List<Type>();
        public static List<Type> TYPES
        {
            get
            {
                if (types.Count <= 0)
                {
                    var dllFiles = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).GetFiles("*.dll").Select(u => u.FullName).ToList();
 
                    dllFiles.ForEach(f =>
                    {
                        try
                        {
                            Assembly assm = Assembly.LoadFrom(f);
                            if (assm != null)
                            {
                                assm.GetTypes().ToList().ForEach(t =>
                                {
                                    DeviceAttribute attr = t.GetCustomAttribute<DeviceAttribute>();
 
                                    if (attr != null)
                                    {
                                        types.Add(t);
 
                                    //if (attr.AttrType == EnumHelper.DeviceAttributeType.Device)
                                    //{
                                    //    DEVICE_TYPE_NAMES.Add(attr.TypeCode);
                                    //    DEVICE_DICT[attr.TypeCode] = t;
                                    //}
                                }
                                });
                            }
                        }
                        catch (Exception)
                        {
                        }
                    });
                }
 
                return types;
            }
        }
 
        //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;
        }
    }
}