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;
|
}
|
}
|
}
|