领胜LDS 键盘AOI检测项目
wells.liu
2020-07-07 5918194fccdb2a2303e713b8d2f3335243b9e2ef
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
        }
    }
}