| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- //----------------------------------------------------------------
- //Module Name: EnumHelper
- //Purpose:
- //CopyRight: Muchinfo
- //History:
- //----------------------------------------------------------------
- //DateTime 2015/1/9 9:48:18
- //Author ouyang.hongbin
- //Description Create
- //----------------------------------------------------------------
- using Muchinfo.MTPClient.Resources;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- namespace Muchinfo.MTPClient.Data.Helper
- {
- /// <summary>
- /// DropDataTemplate,Enum转List Model
- /// </summary>
- public class DropDataTemplate
- {
- private int _Index;
- /// <summary>
- /// 排序
- /// </summary>
- public int Index
- {
- get { return _Index; }
- set { _Index = value; }
- }
- private string _Description;
- /// <summary>
- /// 说明
- /// </summary>
- public string Description
- {
- get { return _Description; }
- set { _Description = value; }
- }
- private int _Value;
- /// <summary>
- /// 值
- /// </summary>
- public int Value
- {
- get { return _Value; }
- set { _Value = value; }
- }
- private string _GroupKey;
- /// <summary>
- /// 分组关键字
- /// </summary>
- public string GroupKey
- {
- get { return _GroupKey; }
- set { _GroupKey = value; }
- }
- private object _Tag;
- /// <summary>
- /// 原先目标
- /// </summary>
- public object Tag
- {
- get { return _Tag; }
- set { _Tag = value; }
- }
- }
- [AttributeUsage(AttributeTargets.Field)]
- public class KindDiscAttribute : Attribute
- {
- private string _Description;
- /// <summary>
- /// Description
- /// </summary>
- public string Description
- {
- get { return _Description; }
- set { _Description = value; }
- }
- private int _Kind;
- /// <summary>
- /// kind
- /// </summary>
- public int Kind
- {
- get { return _Kind; }
- set
- {
- _Kind = Kind;
- }
- }
- /// <summary>
- /// ItemDiscAttribute,kind所属分类
- /// </summary>
- /// <param name="description">说明</param>
- /// <param name="kind">所属分类</param>
- public KindDiscAttribute(string description, int kind)
- {
- _Description = description;
- _Kind = kind;
- }
- /// <summary>
- /// ItemDiscAttribute
- /// </summary>
- /// <param name="Description"></param>
- public KindDiscAttribute(string description)
- {
- _Description = description;
- }
- }
- [AttributeUsage(AttributeTargets.Field)]
- public class ItemDiscAttribute : Attribute
- {
- private string _Description;
- /// <summary>
- /// Description
- /// </summary>
- public string Description
- {
- get { return _Description; }
- set { _Description = Description; }
- }
- private readonly string _Group;
- /// <summary>
- /// getGroup
- /// </summary>
- public string[] Group
- {
- get { return _Group.Split(','); }
- }
- /// <summary>
- /// ItemDiscAttribute,带分组的
- /// </summary>
- /// <param name="description">说明</param>
- /// <param name="Group">分组,如一个Enum同时具有多个分组,请用“,”号隔开</param>
- public ItemDiscAttribute(string description, string group)
- {
- _Description = description;
- _Group = group;
- }
- /// <summary>
- /// ItemDiscAttribute
- /// </summary>
- /// <param name="Description"></param>
- public ItemDiscAttribute(string Description)
- {
- _Description = Description;
- }
- }
- /// <summary>
- /// EnumHelper,枚举工具类
- /// </summary>
- public static class EnumHelper
- {
- private static Dictionary<Enum, string> dictDiscs = new Dictionary<Enum, string>();
- private static Dictionary<string, List<DropDataTemplate>> _EnumList = new Dictionary<string, List<DropDataTemplate>>(); //枚举缓存池
- /// <summary>
- /// 获取枚举说明,
- /// Sim=》TradeDirection.Register.Discription();
- /// </summary>
- /// <param name="myEnum">My enum.</param>
- /// <param name="enumCode">数据库枚举实际定义的代码</param>
- /// <returns>System.String.</returns>
- public static string Discription(this Enum myEnum, string enumCode="")
- {
- string strDisc = string.Empty;
- //if (dictDiscs.ContainsKey(myEnum))
- //{
- // strDisc = dictDiscs[myEnum];
- //}
- //else
- //{
- strDisc = GetDiscription(myEnum, enumCode);
- // dictDiscs.Add(myEnum, strDisc);
- // }
- return strDisc;
- }
- /// <summary>
- /// Gets the discription.
- /// </summary>
- /// <param name="myEnum">My enum.</param>
- /// <param name="enumCode">数据库枚举实际定义的代码</param>
- /// <returns>System.String.</returns>
- private static string GetDiscription(Enum myEnum, string enumCode = "")
- {
- var enumType = myEnum.GetType();
- var val = (int)Enum.Parse(enumType, myEnum.ToString());
- //枚举代码不为空,则根据枚举代码+值从常量缓存获取, 否则根据枚举名称+值从常量数据取
- var resxKey = string.Format("{0}_{1}", string.IsNullOrWhiteSpace(enumCode) ? enumType.Name: enumCode, val);
- if (ConstResxManager.ConstResx != null &&
- ConstResxManager.ConstResx.ResourceHashtable != null &&
- ConstResxManager.ConstResx.ResourceHashtable.ContainsKey(resxKey))
- {
- return ConstResxManager.ConstResx.ResourceHashtable[resxKey] as string;
- }
-
- FieldInfo fieldInfo = enumType.GetField(myEnum.ToString());
- if (fieldInfo == null)
- {
- return myEnum.ToString();
- }
- //获取字典的ItemDisc属性
- object[] attrs = fieldInfo.GetCustomAttributes(typeof(ItemDiscAttribute), true);
- ItemDiscAttribute desc = null;
- if (attrs != null && attrs.Length > 0)
- {
- desc = attrs[0] as ItemDiscAttribute;
- }
- //若ItemDisc为空,则根据枚举的字符从资源里查找对应描述
- string temp = Client_Resource.ResourceManager.GetString(desc == null ? myEnum.ToString() : desc.Description, Client_Resource.Culture);
- if (!string.IsNullOrWhiteSpace(temp)) return temp;
- return myEnum.ToString();
- }
- /// <summary>
- ///显示 枚举内容
- /// </summary>
- /// <typeparam name="T">属性类型</typeparam>
- /// <param name="myEnum"></param>
- /// <returns></returns>
- public static string Discription<T>(this Enum myEnum)
- {
- FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());
- object[] attrs = fieldInfo.GetCustomAttributes(typeof(T), true);
- if (attrs != null && attrs.Length > 0)
- {
- var attributesType = attrs[0].GetType();
- var property = attributesType.GetProperty("Description");
- return (string)property.GetValue(attrs[0], null);
- }
- return myEnum.ToString();
- }
- public static int GetKind(Enum myEnum)
- {
- FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());
- if (fieldInfo != null)
- {
- object[] attrs = fieldInfo.GetCustomAttributes(typeof(KindDiscAttribute), true);
- if (attrs != null && attrs.Length > 0)
- {
- KindDiscAttribute kind = attrs[0] as KindDiscAttribute;
- if (kind != null)
- {
- return kind.Kind;
- }
- }
- }
- return 0;
- }
- /// <summary>
- /// 将枚举转换成 List DropDataTemplate
- /// </summary>
- /// <param name="enumType">枚举类型</param>
- /// <returns></returns>
- public static List<DropDataTemplate> EnumToDictionary(Type enumType)
- {
- string keyName = enumType.FullName;
- if (!_EnumList.ContainsKey(keyName))
- {
- var list = new List<DropDataTemplate>();
- int index = 0;
- foreach (int i in Enum.GetValues(enumType))
- {
- string name = Enum.GetName(enumType, i);
- //取显示名称
- string description = string.Empty;
- object[] atts = enumType.GetField(name).GetCustomAttributes(typeof(ItemDiscAttribute), false);
- if (atts.Length > 0)
- {
- description = ((ItemDiscAttribute)atts[0]).Description;
- }
- list.Add(new DropDataTemplate()
- {
- Index = index++,
- Value = i,
- Description = string.IsNullOrEmpty(description) ? name : description,
- Tag = Enum.Parse(enumType, name),
- });
- }
- object syncObj = new object();
- if (!_EnumList.ContainsKey(keyName))
- {
- lock (syncObj)
- {
- if (!_EnumList.ContainsKey(keyName))
- {
- _EnumList.Add(keyName, list);
- }
- }
- }
- }
- return _EnumList[keyName];
- }
- /// <summary>
- /// 将枚举转换成 List DropDataTemplate
- /// </summary>
- /// <param name="enumType">枚举类型</param>
- /// <param name="groupName">分组名称</param>
- /// <returns></returns>
- public static List<DropDataTemplate> EnumToDictionary(Type enumType, string groupName)
- {
- string keyName = enumType.FullName;
- if (!_EnumList.ContainsKey(keyName))
- {
- var list = new List<DropDataTemplate>();
- int index = 0;
- foreach (int i in Enum.GetValues(enumType))
- {
- string name = Enum.GetName(enumType, i);
- //取显示名称
- string description = string.Empty;
- string group = string.Empty;
- object[] atts = enumType.GetField(name).GetCustomAttributes(typeof(ItemDiscAttribute), false);
- if (!string.IsNullOrWhiteSpace(groupName) && ((ItemDiscAttribute)atts[0]).Group.Where(x => x == groupName).Count() > 0)
- {
- if (atts.Length > 0)
- {
- description = ((ItemDiscAttribute)atts[0]).Description;
- }
- list.Add(new DropDataTemplate()
- {
- Index = index++,
- Value = i,
- GroupKey = groupName,
- Description = string.IsNullOrEmpty(description) ? name : description,
- Tag = Enum.Parse(enumType, name),
- });
- }
- }
- object syncObj = new object();
- if (!_EnumList.ContainsKey(keyName))
- {
- lock (syncObj)
- {
- if (!_EnumList.ContainsKey(keyName))
- {
- _EnumList.Add(keyName, list);
- }
- }
- }
- }
- return _EnumList[keyName];
- }
- /// <summary>
- /// 将枚举转换成 Dictionary<object,string>
- /// </summary>
- /// <param name="enumType">枚举类型</param>
- /// <returns></returns>
- public static Dictionary<object, string> ToDictionaryEnum(Type enumType)
- {
- string keyName = enumType.FullName;
- var list = new Dictionary<object, string>();
- // int index = 0;
- foreach (int i in Enum.GetValues(enumType))
- {
- string name = Enum.GetName(enumType, i);
- //取显示名称
- string description = string.Empty;
- object[] atts = enumType.GetField(name).GetCustomAttributes(typeof(ItemDiscAttribute), false);
- if (atts.Length > 0)
- {
- description = ((ItemDiscAttribute)atts[0]).Description;
- }
- list[Enum.Parse(enumType, name)] = string.IsNullOrEmpty(description) ? name : description;
- }
- return list;
- }
- /// <summary>
- /// 获取枚举值对应的显示名称
- /// </summary>
- /// <param name="enumType">枚举类型</param>
- /// <param name="intValue">枚举项对应的int值</param>
- /// <returns></returns>
- public static
- string GetEnumShowName(Type enumType, int Index)
- {
- return EnumToDictionary(enumType)[Index].Description;
- }
- }
- }
|