解析定义的枚举

 public enum OrderPaymentStatus
{
/// <summary>
/// 未支付
/// </summary>
[Description("未支付")]
No=1,
/// <summary>
/// 已支付
/// </summary>
[Description("已支付")]
Yes
}

解析类

public static class EnumHelper
{
private static Hashtable enumDesciption = EnumHelper.GetDescriptionContainer(); public static string ToDescription(this Enum value)
{
if (value == null)
{
return "";
}
Type type = value.GetType();
string name = Enum.GetName(type, value);
return EnumHelper.GetDescription(type, name);
} public static Dictionary<int, string> ToDescriptionDictionary<TEnum>()
{
Type typeFromHandle = typeof(TEnum);
Array values = Enum.GetValues(typeFromHandle);
Dictionary<int, string> dictionary = new Dictionary<int, string>();
foreach (Enum item in values)
{
dictionary.Add(Convert.ToInt32(item), item.ToDescription());
}
return dictionary;
} public static Dictionary<int, string> ToDictionary<TEnum>()
{
Type typeFromHandle = typeof(TEnum);
Array values = Enum.GetValues(typeFromHandle);
Dictionary<int, string> dictionary = new Dictionary<int, string>();
foreach (Enum item in values)
{
dictionary.Add(Convert.ToInt32(item), item.ToString());
}
return dictionary;
} private static bool IsIntType(double d)
{
return (double)(int)d != d;
} private static Hashtable GetDescriptionContainer()
{
EnumHelper.enumDesciption = new Hashtable();
return EnumHelper.enumDesciption;
} private static void AddToEnumDescription(Type enumType)
{
EnumHelper.enumDesciption.Add(enumType, EnumHelper.GetEnumDic(enumType));
} private static Dictionary<string, string> GetEnumDic(Type enumType)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
FieldInfo[] fields = enumType.GetFields();
FieldInfo[] array = fields;
foreach (FieldInfo fieldInfo in array)
{
if (fieldInfo.FieldType.IsEnum)
{
object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
dictionary.Add(fieldInfo.Name, ((DescriptionAttribute)customAttributes[0]).Description);
}
}
return dictionary;
} private static string GetDescription(Type enumType, string enumText)
{
if (string.IsNullOrEmpty(enumText))
{
return null;
}
if (!EnumHelper.enumDesciption.ContainsKey(enumType))
{
EnumHelper.AddToEnumDescription(enumType);
}
object obj = EnumHelper.enumDesciption[enumType];
if (obj != null && !string.IsNullOrEmpty(enumText))
{
Dictionary<string, string> dictionary = (Dictionary<string, string>)obj;
return dictionary[enumText].Split('|')[0];
}
throw new ApplicationException("不存在枚举的描述");
}
}

  

最新文章

  1. 测试dns
  2. web前端之HTML中元素的区分
  3. (转) The major advancements in Deep Learning in 2016
  4. Open Audio Library
  5. CTF中那些脑洞大开的加密(1)
  6. 【深入Java虚拟机】之一:Java内存模型与内存溢出
  7. (原)String、StringBuilder、StringBuffer作为形参
  8. FastMM配置文件详解
  9. android使用微软雅黑字体
  10. UITableview xib里面 cell 按钮的回调
  11. Bag Problem
  12. shell 外部传入jmeter脚本线程数,rampUp时间,持续运行时间
  13. Spring + Tomcat 启动报错java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
  14. Windows文件居然有解锁一说,并且还会引起SignTool Error,真是昏倒!
  15. Codeforces Round #350 (Div. 2)解题报告
  16. 怎么做fastreport使用离线数据源
  17. Bill Total Value
  18. hibernate---一对一双向外键关联 (重要)
  19. eval()和$.parseJSON()注意事项
  20. idea 设置选中代码得背景颜色

热门文章

  1. textarea使换行变顿号
  2. 可枚举接口的知识点(IEnumerable 接口)要使用foreach,就必须实现可枚举接口
  3. oracle 导出空表问题
  4. openmax component类的继承关系
  5. 使用 JavaScript 实现名为 flatten(input) 的函数,可以将传入的 input 对象(Object 或者 Array)进行扁平化处理并返回结果
  6. insert插入数据
  7. 网站程序application, session, cookie区别的一点小总结
  8. 关于Date的冷门知识记录
  9. .Net core 下Swagger如何隐藏接口的显示
  10. hibernate的报错信息a different object with the same identifier value was already associated with the session解决办法