领胜LDS 键盘AOI检测项目
xcd
2020-07-02 c866d11e0054a7299076fa53830b96610286ac2c
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
100
101
102
103
104
105
106
using Bro.Common.Interface;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
 
namespace Bro.Common.Helper
{
    public class StationFactory
    {
        /// <summary>
        /// 获取StationProcess
        /// </summary>
        /// <param name="stationCode">站点编号 S1~S7</param>
        /// <param name="userName">登录用户名称</param>
        /// <param name="msg">异常信息</param>
        /// <returns></returns>
        public static IProcess CreateStationProcess(string stationCode, string userName, out string msg)
        {
            IProcess proc = null;
            msg = "";
 
            try
            {
                var dllFiles = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).GetFiles("Bro.*.dll").Select(u => u.FullName).ToList();
                
                foreach (string f in dllFiles)
                {
                    try
                    {
                        Assembly assm = Assembly.LoadFrom(f);
                        if (assm != null)
                        {
                            foreach (Type t in assm.GetTypes())
                            {
                                if (typeof(IProcess).IsAssignableFrom(t))
                                {
                                    var attr = t.GetCustomAttribute<ProcessAttribute>();
                                    if (attr != null && attr.ProcessCode == stationCode)
                                    {
                                        proc = Activator.CreateInstance(t, userName) as IProcess;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex) { }
                }
            }
            catch (Exception ex)
            {
                msg = ex.GetExceptionMessage();
            }
 
            return proc;
        }
 
        /// <summary>
        /// 获取StationConfig
        /// </summary>
        /// <param name="stationCode">站点编号 S1~S7</param>
        /// <param name="msg">异常信息</param>
        /// <returns></returns>
        public static IProcessConfig CreateStationConfig(string stationCode, out string msg)
        {
            IProcessConfig config = null;
            msg = "";
 
            try
            {
                var dllFiles = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).GetFiles("Bro.*.dll").Select(u => u.FullName).ToList();
 
                foreach (string f in dllFiles)
                {
                    try
                    {
                        Assembly assm = Assembly.LoadFrom(f);
                        if (assm != null)
                        {
                            foreach (Type t in assm.GetTypes())
                            {
                                if (typeof(IProcessConfig).IsAssignableFrom(t))
                                {
                                    var attr = t.GetCustomAttribute<ProcessAttribute>();
                                    if (attr != null && attr.ProcessCode == stationCode)
                                    {
                                        config = Activator.CreateInstance(t) as IProcessConfig;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex) { }
                }
            }
            catch (Exception ex)
            {
                msg = ex.GetExceptionMessage();
            }
 
            return config;
        }
    }
}