/** * * @date 2014-10-22 * * @author 邓尹平 * * @par 成员列表类 说明 * 主要负责管理成员 提供搜索\排序和操作的方法 * * @par 版权声明 * 深圳市多元世纪信息技术有限公司 版权所有 * * @see 使用此类时参照一些其他类可以写在这里 * * @todo 该类有待完成的任务 一条条列出 完成一条删除一条 * * @bug 该类已知的Bug一条条列出 完成一条删除一条 * */ using Muchinfo.MTPClient.Data.Helper; using Muchinfo.MTPClient.Resources; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Muchinfo.MTPClient.Data.Extensions { public static class EnumExtensions { private static Dictionary dictDiscs = new Dictionary(); private static Dictionary> _EnumList = new Dictionary>(); //枚举缓存池 /// /// 获取枚举描述 /// /// The en. /// System.String. public static string ToDescription(this Enum en) { try { Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(ItemDiscAttribute), false); if (attrs.Length > 0) return ((ItemDiscAttribute)attrs[0]).Description; } return en.ToString(); } catch { return string.Empty; } } /// /// 获取枚举说明, /// Sim=》TradeDirection.Register.Discription(); /// /// /// public static string Discription1(this Enum myEnum) { string strDisc = string.Empty; if (dictDiscs.ContainsKey(myEnum)) { strDisc = dictDiscs[myEnum]; } else { strDisc = GetDiscription(myEnum); dictDiscs.Add(myEnum, strDisc); } return strDisc; } private static string GetDiscription(Enum myEnum) { FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString()); if (fieldInfo == null) { return myEnum.ToString(); } object[] attrs = fieldInfo.GetCustomAttributes(typeof(ItemDiscAttribute), true); if (attrs != null && attrs.Length > 0) { ItemDiscAttribute desc = attrs[0] as ItemDiscAttribute; if (desc != null) { string temp = Client_Resource.ResourceManager.GetString(desc.Description, Client_Resource.Culture); return temp; } } return myEnum.ToString(); } /// /// 枚举转换成字典Key-枚举值, Value-枚举描述 /// /// The type of the t enum. /// Dictionary{``0System.String}. public static Dictionary ToDictionary() where TEnum : struct { try { var result = new Dictionary(); foreach (var item in Enum.GetValues(typeof(TEnum))) { var enumItem = (Enum)item; result.Add((TEnum)item, enumItem.Discription()); } return result.OrderBy(z => z.Key).ToDictionary(z => z.Key, z => z.Value); } catch { return null; } } } }