using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dic1 = GetEnumItemDesc(typeof(Days)); foreach (string key in dic1.Keys)
{
Console.WriteLine(key + ":{0}", dic1[key]);
} Dictionary<string, string> dic = GetEnumItemValueDesc(typeof(Days));
foreach (string key in dic.Keys)
{
Console.WriteLine(key + ":{0}", dic[key]);
} Console.WriteLine(string.Format(Days.Sunday.ToString() + ":{0}", GetEnumDesc(Days.Sunday)));
Console.WriteLine(string.Format("{0}", (int)Enum.Parse(typeof(Days), "Thursday", true)));
Console.ReadKey();
} /// <summary>
/// 获取枚举项描述信息 例如GetEnumDesc(Days.Sunday)
/// </summary>
/// <param name="en">枚举项 如Days.Sunday</param>
/// <returns></returns>
public static string GetEnumDesc(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
return ((DescriptionAttribute)attrs[0]).Description;
}
return en.ToString();
} ///<summary>
/// 获取枚举项+描述
///</summary>
///<param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
///<returns>键值对</returns>
public static Dictionary<string, string> GetEnumItemDesc(Type enumType)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
FieldInfo[] fieldinfos = enumType.GetFields();
foreach (FieldInfo field in fieldinfos)
{
if (field.FieldType.IsEnum)
{
Object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
dic.Add(field.Name, ((DescriptionAttribute)objs[0]).Description);
}
}
return dic;
} ///<summary>
/// 获取枚举值+描述
///</summary>
///<param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
///<returns>键值对</returns>
public static Dictionary<string, string> GetEnumItemValueDesc(Type enumType)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
Type typeDescription = typeof(DescriptionAttribute);
FieldInfo[] fields = enumType.GetFields();
string strText = string.Empty;
string strValue = string.Empty;
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{
strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
object[] arr = field.GetCustomAttributes(typeDescription, true);
if (arr.Length > 0)
{
DescriptionAttribute aa = (DescriptionAttribute)arr[0];
strText = aa.Description;
}
else
{
strText = field.Name;
}
dic.Add(strValue, strText);
}
}
return dic;
}
} public enum Days
{
[Description("星期天")]
Sunday,
[Description("星期一")]
Monday,
[Description("星期二")]
Tuesday,
[Description("星期三")]
Wednesday,
[Description("星期四")]
Thursday,
[Description("星期五")]
Friday,
[Description("星期六")]
Saturday
}
}

c#枚举 获取枚举键值对、描述等的更多相关文章

  1. Python3+SQLAlchemy不使用字段名获取主键值教程

    一.说明 1.1 环境说明 user model如下,且其现有一个实例user_inst: class User(Base): __tablename__ = 'users' username = C ...

  2. mybatis插入数据并获取主键值

    有时候我们的主键是自增的,但是我们想要在插入一条数据以后获取这条数据的主键值,而我们知道,mybatis执行完插入操作以后返回的是生效的记录数.那如何才能获取这个主键值呢. 1.在配置文件mapper ...

  3. 根据枚举获取枚举的Description特性值

    首先定义一个枚举:两个值:已确认.未确认. public enum ConfirmStatusEnum { [Description("未确认")] unconfirmed = , ...

  4. js原生_获取url键值对

    思路: 1.先对url进行处理,获取 ?后的字符串 postid=10457794&actiontip=保存修改成功') 2. 字符串通过&标识,不同参数转为数组 ["pos ...

  5. c#所有部门及其下所部门生成树形图(递归算法获取或键值对方式获取)

    部门数据库的设计: 代码: /// <summary> /// 获取部门(入口) /// </summary> /// <returns></returns& ...

  6. mybatis oracle 插入自增记录 获取主键值 写回map参数

    网上搜了好多文章照着弄都返回不了主键给map, 实践证明要在传入的map参数里写回插入的主键,要这样写 <selectKey resultType="java.lang.Integer ...

  7. 获取json键值对的对应字符串

    获取json中的姓名 json串ac 关键字key public class Json { public static String json(String  key;String  ac) { JS ...

  8. C语言定义从URL中获取键值的接口

    环境:centos7下,对客户端http请求进行解析,来获取有效键值(包括汉字). 头文件 /* 这是一份关于从Http请求信息中提取键值的接口声明的头文件 */ #ifndef _HEAD_H_ # ...

  9. Statement和PreparedStatement的特点 MySQL数据库分页 存取大对象 批处理 获取数据库主键值

    1 Statement和PreparedStatement的特点   a)对于创建和删除表或数据库,我们可以使用executeUpdate(),该方法返回0,表示未影向表中任何记录   b)对于创建和 ...

随机推荐

  1. web切图的几个快捷键及总结

      自由的控制视图系列 自由的浏览图片:按住空格后,鼠标拖动 自由的缩放图片:按住alt+滚轮 切换到实际像素(100%):Ctrl + 1 工作区类型切换:Tab 控制内容系列 通过画面上点选一个图 ...

  2. Swing-JCheckBox用法-入门

    JCheckBox是Swing中的复选框.所谓复选框就是指,可以同时存在多个这样的控件,它们可以有多个处于被选中状态.对于每一个复选框而言,它只有选中和未选中两种状态. JCheckBox的常用方法如 ...

  3. 201521123017 《Java程序设计》第8周学习总结

    1. 本周学习总结 2. 书面作业 Q1.List中指定元素的删除(题目4-1) 1.1 实验总结 for (int i = list.size()-1; i >=0; i--) {//从最后一 ...

  4. Java第七周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 ...

  5. VBScript中Msgbox函数的用法

    MsgBox(prompt[, buttons][, title][, helpfile, context]) [用途]:弹出对话框,并获取用户的操作结果. [参数说明]: propmt:对话框中展示 ...

  6. 201521123011《Java程序设计》第10周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...

  7. 201521123074 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 Q1. 网络基础 参考:实验任务书-题目1 1.1 比较ping www.baidu.co ...

  8. 域名解析>>"记录类型" 说明

    (1)A (Address) 记录是用来指定主机名(或域名)对应的IP地址记录. 说明:用户可以将该域名下的网站服务器指向到自己的web server上.同时也可以设置自己域名的二级域名. (2)MX ...

  9. spring boot / cloud (十七) 快速搭建注册中心和配置中心

    spring boot / cloud (十七) 快速搭建注册中心和配置中心 本文将使用spring cloud的eureka和config server来搭建. 然后搭建的模式,有很多种,本文主要聊 ...

  10. Redis总结(六)Redis配置文件全解

    前面已经写了一些关于redis 的介绍,redis 的基本功能和用法,基本上都说了,有问题的可以去看看 http://www.cnblogs.com/zhangweizhong/category/77 ...