using Bro.Common.Helper; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; namespace Bro.Common.Factory { public static class FactoryHelper { const string DLLPATTERN = "Bro.*.dll"; private static List types = new List(); public static List TYPES { get { if (types.Count <= 0) { types = GetAttributeType().Values.ToList(); } return types; } } public static Dictionary GetAttributeType() where T : Attribute { Dictionary attrTypeDict = new Dictionary(); var dllFiles = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).GetFiles(DLLPATTERN).Select(u => u.FullName).ToList(); dllFiles.ForEach(f => { try { Assembly assm = Assembly.LoadFrom(f); if (assm != null) { assm.GetTypes().ToList().ForEach(t => { T attr = t.GetCustomAttribute(); if (attr != null) { attrTypeDict[attr] = t; } }); } } catch (Exception) { } }); return attrTypeDict; } //private static Dictionary device_dict = new Dictionary(); //public static Dictionary DEVICE_DICT //{ // get // { // if (device_dict.Count < 0) // { // } // return device_dict; // } //} //private static List device_type_names = new List(); //public static List DEVICE_TYPE_NAMES //{ // get // { // if (device_type_names.Count < 0) // { // } // return device_type_names; // } //} public static Type GetTypeByAtrributeTypeName(string attrTypeName, EnumHelper.DeviceAttributeType attrType) { Type type = TYPES.FirstOrDefault(t => { DeviceAttribute attr = t.GetCustomAttribute(); return attr != null && attr.TypeCode == attrTypeName && attr.AttrType == attrType; }); return type; } } }