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
{
///
/// 根据设备类型名称获取设备对象
///
/// 设备类型名称,如HalconCamera
///
//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(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 GetAllSupportDeviceTypeNames(Type instanceBase = null)
{
List deviceNames = new List();
FactoryHelper.TYPES.ForEach(t =>
{
var attr = t.GetCustomAttribute();
if (attr.AttrType == DeviceAttributeType.Device)
{
if (instanceBase != null)
{
if (t.IsSubclassOf(instanceBase))
{
deviceNames.Add(attr.TypeCode);
}
}
else
{
deviceNames.Add(attr.TypeCode);
}
}
});
return deviceNames;
}
}
}