添加项目文件。

This commit is contained in:
17860779768
2023-03-24 09:58:42 +08:00
parent 2c3a2dbe4f
commit 03e8e92c40
38 changed files with 2716 additions and 0 deletions

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
namespace XKRS.Common.Factory
{
public static class FactoryHelper
{
const string DLLPATTERN = "XKRS.*.dll";
//readonly指示只能在声明期间或在同一个类的构造函数中向字段赋值
//可以在字段声明和构造函数中多次分配和重新分配只读字段。
//构造函数退出后,不能分配 readonly 字段。
private static readonly string[] DLLPATTERNS = { "XKRS." };
public static Dictionary<T, Type> GetAttributeType<T>() where T : Attribute
{
Dictionary<T, Type> attrTyprDict = new Dictionary<T, Type>();//创建一个键值对集合
var dllFiles = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory)
.GetFiles("*.dll")
.Where(fi => DLLPATTERNS.Any(pattern => fi.Name.StartsWith(pattern)))
.Select(u =>
{
try
{
return u.FullName;//获取完整目录
}
catch { }
return string.Empty;//返回一个只读的空字符串,
})
.Where(s => !string.IsNullOrEmpty(s))
.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)
{
attrTyprDict[attr] = t;
}
});
}
}
catch(Exception)
{
}
});
return attrTyprDict;
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XKRS.Common.Interface;
using XKRS.Common.Model.Helper;
namespace XKRS.Common.Factory
{
public static class ProcessFactory
{
/// <summary>
/// 获取项目中所有流程,即寻找所有带有[Process]的
/// </summary>
/// <returns>返回流程名称列表</returns>
public static List<string> GetProcessCodes()
{
var attrs = FactoryHelper.GetAttributeType<ProcessAttribute>().Keys;
List<string> processCodes = attrs.Select(u => u.ProcessCode).Distinct().ToList();
return processCodes;
}
/// <summary>
/// 获取StationProcess
/// </summary>
/// <param name="processCode">流程代码</param>
/// <param name="productionCode"></param>
/// <param name="msg">异常信息</param>
/// <returns></returns>
public static IProcess CreateStationProcess(string processCode,string productionCode,out string msg)
{
IProcess proc = null;
msg = "";
try
{
var typeDict = FactoryHelper.GetAttributeType<ProcessAttribute>();
foreach(KeyValuePair<ProcessAttribute,Type>pair in typeDict)
{
if (typeof(IProcess).IsAssignableFrom(pair.Value) && pair.Key.ProcessCode == processCode)
{
proc = Activator.CreateInstance(pair.Value, productionCode) as IProcess;
break;
}
}
}
catch(Exception ex)
{
msg = ex.GetExceptionMessage();
}
return proc;
}
}
}