领胜LDS 键盘AOI检测项目
wells.liu
2020-06-29 c339e592f9232e460602093cb3248adf2484eebb
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
using Bro.Common.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
 
namespace Bro.UI.Model.Winform
{
    public static class MenuFormFactory
    {
        const string DLLPATTERN = "Bro.*.dll";
 
        private static Dictionary<MenuNodeAttribute, Type> menuFrmTypeDict = null;
        public static Dictionary<MenuNodeAttribute, Type> MenuFrmTypeDict
        {
            get
            {
                if (menuFrmTypeDict == null)
                {
                    var assm = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).GetFiles(DLLPATTERN).Select(u => Assembly.LoadFrom(u.FullName)).ToList();
                    assm.AddRange(AppDomain.CurrentDomain.GetAssemblies());
                    assm = assm.Distinct().ToList();
 
                    //var types = assm.SelectMany(a => a.GetTypes());
 
                    menuFrmTypeDict = assm.SelectMany(a => a.GetTypes())
                                .Where(t =>
                                {
                                    if (t.GetInterfaces().Contains(typeof(IMenuNode)))
                                    {
                                        var attr = t.GetCustomAttribute<MenuNodeAttribute>();
                                        return attr != null;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                }).ToDictionary(t => t.GetCustomAttribute<MenuNodeAttribute>(), t => t);
                }
 
                return menuFrmTypeDict;
            }
        }
 
        public static MenuFrmBase GetMenuFrm(string frmCode)
        {
            Type frmType = GetMenuFrmType(frmCode);
 
            if (frmType == null)
            {
                return null;
            }
 
            var menu = Activator.CreateInstance(frmType) as MenuFrmBase;
            menu.Tag = frmCode;
            return menu;
        }
 
        public static Type GetMenuFrmType(string frmCode)
        {
            Type frmType = null;
            foreach (KeyValuePair<MenuNodeAttribute, Type> pair in MenuFrmTypeDict)
            {
                if (pair.Key.MenuCode == frmCode)
                {
                    frmType = pair.Value;
                    break;
                }
            }
 
            return frmType;
        }
    }
}