202303241701Change
This commit is contained in:
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using XKRS.Common.Model.Helper;
|
||||
|
||||
namespace XKRS.Common.Factory
|
||||
{
|
||||
@ -16,7 +17,18 @@ namespace XKRS.Common.Factory
|
||||
//构造函数退出后,不能分配 readonly 字段。
|
||||
private static readonly string[] DLLPATTERNS = { "XKRS." };
|
||||
|
||||
|
||||
private static List<Type> types = new List<Type>();
|
||||
public static List<Type> TYPES
|
||||
{
|
||||
get
|
||||
{
|
||||
if (types.Count <= 0)
|
||||
{
|
||||
types = GetAttributeType<DeviceAttribute>().Values.ToList();
|
||||
}
|
||||
return types;
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<T, Type> GetAttributeType<T>() where T : Attribute
|
||||
{
|
||||
@ -59,5 +71,15 @@ namespace XKRS.Common.Factory
|
||||
});
|
||||
return attrTyprDict;
|
||||
}
|
||||
|
||||
public static Type GetTypeByAttributeTypeName(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
26
XKRS.Common.Model/Factory/UIFactory.cs
Normal file
26
XKRS.Common.Model/Factory/UIFactory.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using XKRS.Common.Interface;
|
||||
using XKRS.Common.Model.Helper;
|
||||
|
||||
namespace XKRS.Common.Factory
|
||||
{
|
||||
public class UIFactory
|
||||
{
|
||||
public static IRunCtrl GetRunCtrl(IDevice device)
|
||||
{
|
||||
var attr = device.GetType().GetCustomAttribute<DeviceAttribute>();
|
||||
if (attr == null)
|
||||
return null;
|
||||
var type = FactoryHelper.GetTypeByAttributeTypeName(attr.TypeCode, EnumHelper.DeviceAttributeType.RunCtrl);
|
||||
if (type == null)
|
||||
return null;
|
||||
IRunCtrl runCtrl = Activator.CreateInstance(type, new object[] { device }) as IRunCtrl;
|
||||
return runCtrl;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user