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<Type> types = new List<Type>();
|
public static List<Type> TYPES
|
{
|
get
|
{
|
if (types.Count <= 0)
|
{
|
types = GetAttributeType<DeviceAttribute>().Values.ToList();
|
}
|
|
return types;
|
}
|
}
|
|
public static Dictionary<T, Type> GetAttributeType<T>() where T : Attribute
|
{
|
Dictionary<T, Type> attrTypeDict = new Dictionary<T, Type>();
|
|
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<T>();
|
|
if (attr != null)
|
{
|
attrTypeDict[attr] = t;
|
}
|
});
|
}
|
}
|
catch (Exception)
|
{
|
}
|
});
|
|
return attrTypeDict;
|
}
|
|
//private static Dictionary<string, Type> device_dict = new Dictionary<string, Type>();
|
//public static Dictionary<string, Type> DEVICE_DICT
|
//{
|
// get
|
// {
|
// if (device_dict.Count < 0)
|
// {
|
|
// }
|
|
// return device_dict;
|
// }
|
//}
|
|
//private static List<string> device_type_names = new List<string>();
|
//public static List<string> 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<DeviceAttribute>();
|
return attr != null && attr.TypeCode == attrTypeName && attr.AttrType == attrType;
|
});
|
|
return type;
|
}
|
}
|
}
|