| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /**
- *
- * @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<Enum, string> dictDiscs = new Dictionary<Enum, string>();
- private static Dictionary<string, List<DropDataTemplate>> _EnumList = new Dictionary<string, List<DropDataTemplate>>(); //枚举缓存池
- /// <summary>
- /// 获取枚举描述
- /// </summary>
- /// <param name="en">The en.</param>
- /// <returns>System.String.</returns>
- 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;
- }
- }
- /// <summary>
- /// 获取枚举说明,
- /// Sim=》TradeDirection.Register.Discription();
- /// </summary>
- /// <param name="myEnum"></param>
- /// <returns></returns>
- 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();
- }
- /// <summary>
- /// 枚举转换成字典Key-枚举值, Value-枚举描述
- /// </summary>
- /// <typeparam name="TEnum">The type of the t enum.</typeparam>
- /// <returns>Dictionary{``0System.String}.</returns>
- public static Dictionary<TEnum, string> ToDictionary<TEnum>() where TEnum : struct
- {
- try
- {
- var result = new Dictionary<TEnum, string>();
- 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;
- }
- }
- }
- }
|