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 menuFrmTypeDict = null; public static Dictionary 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(); return attr != null; } else { return false; } }).ToDictionary(t => t.GetCustomAttribute(), 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 pair in MenuFrmTypeDict) { if (pair.Key.MenuCode == frmCode) { frmType = pair.Value; break; } } return frmType; } } }