解析定义的枚举

 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("不存在枚举的描述");
}
}

  

c# enum 解析的更多相关文章

  1. Java Enum解析【转】

    Enum用法: 1:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多 ...

  2. Java基础系列-Enum深入解析

    原创文章,转载请标注出处:https://www.cnblogs.com/V1haoge/p/10755129.html 一.概述 枚举就是一个语法糖效果. 定义一个枚举,其实就是定义一个继承抽象类E ...

  3. Java中enum的学习总结

    一.通常的定义常量的方法 public class Sex{ public final static int MALE = 1; public final static int FEMALE=2; } ...

  4. 深度解析开发项目之 03 - enum的使用

    深度解析开发项目之 03 - enum的使用 01 - 在#import和@interface之间定义typedef enum 注意: 默认是0,1,2,3 02 - 定义可以操作的数据类型的属性 0 ...

  5. 设计模式课程 设计模式精讲 8-8 单例设计模式-Enum枚举单例、原理源码解析以及反编译实战

    1 课堂解析 2 代码演练 2.1 枚举类单例解决序列化破坏demo 2.2 枚举类单例解决序列化破坏原理 2.3 枚举类单例解决反射攻击demo 2.4 枚举类单例解决反射攻击原理 3 jad的使用 ...

  6. 【Winform】 Enum逆向解析

    将字符串转换成Enum类型 Enum.Parse:将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象.   名称 说明   Parse(Type, String) 将一个或多个枚举常数 ...

  7. swift的enum模式—对Alamofire入口的解析--数据结构与操作结合的模式

    swift的枚举模式是数据结构与操作结合的模式 1.enum本质是一个类型,可以定义变量: 它定义的变量可以用到其它变量用的的任何地方:函数的输入.输出.成员变量.临时变量等: 这个变量还可以带有附加 ...

  8. EffectiveJava(30) -- 全面解析enum类型

    --在大多数项目中,我们会经常使用int类型来声明final类型的常量,它在不考虑安全的情况下确实能满足我们绝大多数的需求.但是在JDK1.5版本发布之后,声明一组固定的常量组成合法值的类型就建议使用 ...

  9. 【算法】(查找你附近的人) GeoHash核心原理解析及代码实现

    本文地址 原文地址 分享提纲: 0. 引子 1. 感性认识GeoHash 2. GeoHash算法的步骤 3. GeoHash Base32编码长度与精度 4. GeoHash算法 5. 使用注意点( ...

随机推荐

  1. 将数组打印到txt文件中

    用print_r 将数组打印到txt文件中.     1.function save_log($content='', $file='app') { $logDir = './logs'; $now ...

  2. mysql学习之join用法

    转载  一张图看懂 SQL 的各种 join 用法 一.JOIN 使用介绍 下面例子使用的数据表如下: -- ---------------------------- -- Table structu ...

  3. 新建oracle实例

    1.安装好ORACLE服务端.2.在开始菜单中,点击ORAHOME目录下的"Configuration and Migration Tools"下的"Database C ...

  4. mysql错误errno:121

    121错误是因为外键名重复.在同一个库中外键是不允许与其他外键重名的. 遇到这个错误请给你定义的外键换唯一无重复的名字. 同时查阅到外键也有可能导致150错误. Can't create table ...

  5. maven 环境变量 设置

    Maven安装与配置   一.需要准备的东西 1. JDK 2. Eclipse 3. Maven程序包 二.下载与安装 1. 前往https://maven.apache.org/download. ...

  6. oracle database 9i/10g/11g 编程艺术 源代码下载

    背景 在找这本书的源码,搜到提供的都是需要C币下载的.比较固执(其实是穷). 在这本书的前言中提到源代码可以在 www.appress.com 上下载. 下面是该书在该网站上的链接: https:// ...

  7. 搭建Extjs框架(一)

    搭建Extjs框架 pc端 github https://github.com/Status400/Extjs-6.2.0-demo   欢迎start 准本工作:       官方下载Extjs  ...

  8. Wireshark抓取Mqtt报文

    安装版本较高的Wireshark,我的版本是2.4.6,然后在编辑--> 首选项--> 协议中找到MQTT,然后将端口改为你MQTT服务器的端口,然后就可以在抓包中抓到MQTT了

  9. .Net 上传文件到ftp服务器和下载文件

    突然发现又很久没有写博客了,想起哎呦,还是写一篇博客记录一下吧,虽然自己还是那个渣渣猿. 最近在做上传文件的功能,上传到ftp文件服务器有利于管理上传文件. 前面的博客有写到layui如何上传文件,然 ...

  10. springboot-自定义起步依赖

    自定义起步依赖步骤: 1.  添加configuration注解文件 -          指定什么情况下加载配置 -          使用enableconfigurationProperties ...