1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using Bro.Common.Helper;
using Bro.Common.Interface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
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 List<string> GetAllSupportDeviceTypeNames()
        {
            List<string> deviceNames = new List<string>();
            FactoryHelper.TYPES.ForEach(t =>
            {
                var attr = t.GetCustomAttribute<DeviceAttribute>();
 
                if (attr.AttrType == DeviceAttributeType.Device)
                {
                    deviceNames.Add(attr.TypeCode);
                }
            });
 
            return deviceNames;
        }
    }
}