using Bro.Common.Helper;
|
using Bro.Common.Interface;
|
using System;
|
using System.Collections.Generic;
|
using System.Reflection;
|
using static Bro.Common.Helper.EnumHelper;
|
|
namespace Bro.Common.Factory
|
{
|
public static class DeviceFactory
|
{
|
/// <summary>
|
/// 根据设备类型名称获取设备对象
|
/// </summary>
|
/// <param name="deviceTypeName">设备类型名称,如HalconCamera</param>
|
/// <returns></returns>
|
//public static IDevice GetDeviceInstanceByTypeName(string deviceTypeName)
|
//{
|
// Type deviceType = FactoryHelper.GetTypeByAtrributeTypeName(deviceTypeName, DeviceAttributeType.Device);
|
|
// if (deviceType == null)
|
// {
|
// return null;
|
// }
|
|
// return Activator.CreateInstance(deviceType) as IDevice;
|
//}
|
|
public static IDevice GetDeviceInstanceByTypeName(string deviceTypeName)
|
{
|
Type deviceType = FactoryHelper.GetTypeByAtrributeTypeName(deviceTypeName, DeviceAttributeType.Device);
|
|
if (deviceType == null)
|
{
|
return null;
|
}
|
|
return Activator.CreateInstance(deviceType) as IDevice;
|
}
|
|
public static T GetDeviceInstanceByTypeName<T>(string deviceTypeName) where T : class, IDevice
|
{
|
Type deviceType = FactoryHelper.GetTypeByAtrributeTypeName(deviceTypeName, DeviceAttributeType.Device);
|
|
if (deviceType == null)
|
{
|
return null;
|
}
|
|
return Activator.CreateInstance(deviceType) as T;
|
}
|
|
public static List<string> GetAllSupportDeviceTypeNames(Type instanceBase = null)
|
{
|
List<string> deviceNames = new List<string>();
|
FactoryHelper.TYPES.ForEach(t =>
|
{
|
var attr = t.GetCustomAttribute<DeviceAttribute>();
|
|
if (attr.AttrType == DeviceAttributeType.Device)
|
{
|
if (instanceBase != null)
|
{
|
if (t.IsSubclassOf(instanceBase))
|
{
|
deviceNames.Add(attr.TypeCode);
|
}
|
}
|
else
|
{
|
deviceNames.Add(attr.TypeCode);
|
}
|
}
|
});
|
|
return deviceNames;
|
}
|
}
|
}
|