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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
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 IStationProcess CreateStationProcess(string stationCode, string userName, out string msg)
        {
            IStationProcess proc = null;
            msg = "";
 
            try
            {
                var assList = AppDomain.CurrentDomain.GetAssemblies().ToList();
                var assNameList = assList.Select(a => a.GetName()).ToList();
 
                Assembly ass = assList.FirstOrDefault(a => a.GetName().Name.Contains("Bro.Device.Station"));
                if (ass == null)
                {
                    ass = Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bro.Device.Station.dll"));
                }
 
                ass.GetReferencedAssemblies().ToList().ForEach(a =>
                {
                    if (!assNameList.Contains(a))
                        Assembly.Load(a);
                });
 
                ass.GetTypes().ToList().ForEach(t =>
                {
                    if (typeof(IStationProcess).IsAssignableFrom(t))
                    {
                        var attr = t.GetCustomAttribute<StationAttribute>();
                        if (attr != null && attr.StationCode == stationCode)
                        {
                            //proc = (Activator.CreateInstance(t, userName) as IStationProcess).GetInstance();
                            proc = Activator.CreateInstance(t, userName) as IStationProcess;
                            return;
                        }
                    }
                });
            }
            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 IStationConfig CreateStationConfig(string stationCode, out string msg)
        {
            IStationConfig config = null;
            msg = "";
 
            try
            {
                Assembly ass = Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bro.Device.Station.dll"));
 
                ass.GetReferencedAssemblies().ToList().ForEach(a =>
                {
                    Assembly.Load(a);
                });
 
                ass.GetTypes().ToList().ForEach(t =>
                {
                    if (typeof(IStationConfig).IsAssignableFrom(t))
                    {
                        var attr = t.GetCustomAttribute<StationAttribute>();
                        if (attr != null && attr.StationCode == stationCode)
                        {
                            config = Activator.CreateInstance(t) as IStationConfig;
                            return;
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                msg = ex.GetExceptionMessage();
            }
 
            return config;
        }
    }
}