c#枚举 获取枚举键值对、描述等
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#枚举 获取枚举键值对、描述等的更多相关文章
- Python3+SQLAlchemy不使用字段名获取主键值教程
一.说明 1.1 环境说明 user model如下,且其现有一个实例user_inst: class User(Base): __tablename__ = 'users' username = C ...
- mybatis插入数据并获取主键值
有时候我们的主键是自增的,但是我们想要在插入一条数据以后获取这条数据的主键值,而我们知道,mybatis执行完插入操作以后返回的是生效的记录数.那如何才能获取这个主键值呢. 1.在配置文件mapper ...
- 根据枚举获取枚举的Description特性值
首先定义一个枚举:两个值:已确认.未确认. public enum ConfirmStatusEnum { [Description("未确认")] unconfirmed = , ...
- js原生_获取url键值对
思路: 1.先对url进行处理,获取 ?后的字符串 postid=10457794&actiontip=保存修改成功') 2. 字符串通过&标识,不同参数转为数组 ["pos ...
- c#所有部门及其下所部门生成树形图(递归算法获取或键值对方式获取)
部门数据库的设计: 代码: /// <summary> /// 获取部门(入口) /// </summary> /// <returns></returns& ...
- mybatis oracle 插入自增记录 获取主键值 写回map参数
网上搜了好多文章照着弄都返回不了主键给map, 实践证明要在传入的map参数里写回插入的主键,要这样写 <selectKey resultType="java.lang.Integer ...
- 获取json键值对的对应字符串
获取json中的姓名 json串ac 关键字key public class Json { public static String json(String key;String ac) { JS ...
- C语言定义从URL中获取键值的接口
环境:centos7下,对客户端http请求进行解析,来获取有效键值(包括汉字). 头文件 /* 这是一份关于从Http请求信息中提取键值的接口声明的头文件 */ #ifndef _HEAD_H_ # ...
- Statement和PreparedStatement的特点 MySQL数据库分页 存取大对象 批处理 获取数据库主键值
1 Statement和PreparedStatement的特点 a)对于创建和删除表或数据库,我们可以使用executeUpdate(),该方法返回0,表示未影向表中任何记录 b)对于创建和 ...
随机推荐
- 移动端rem使用
let $html=document.documentElement,windowW = window.innerWidth,ratio = windowW / 750if (windowW > ...
- hdu 3342 拓扑排序 水
好久没切题 先上水题! 拓扑排序! 代码: #include<iostream> #include<cstdio> #include<cstring> using ...
- NHibernate教程(7)--并发控制
本节内容 什么是并发控制? 悲观并发控制(Pessimistic Concurrency) 乐观并发控制(Optimistic Concurrency) NHibernate支持乐观并发控制 实例分析 ...
- 软工+C(2017第6期) 最近发展区/脚手架
// 上一篇:工具和结构化 // 下一篇:野生程序员 教育心理学里面有提到"最近发展区"这个概念,这个概念是前苏联发展心理学家维果茨基(Vygotsky)提出的,英文名词是Zone ...
- html5 javascript 小型计算器
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 集美大学网络1413第七次作业成绩(团队三) --需求改进&系统设计
题目 团队作业3--需求改进&系统设计 团队作业3成绩 团队/分值 TD BZ GJ CJ SI WBS GS JG DB SS SJ CS DC 总分 1 0.25 0.75 1 0.5 ...
- java--整理下关于static关键字的知识
如果将域定义为static,每个类中只有一个这样的域.而每一个对象对于所有的实例域却都有自己的一份拷贝.--<java核心技术> 使用static的两种情形:1.只想为某特定域分配单一存储 ...
- 201521123061 《Java程序设计》第八周学习总结
201521123061 <Java程序设计>第八周学习总结 1. 本周学习总结 2. 书面作业 1.List中指定元素的删除(题目4-1) 1.1 实验总结 主要是应用到了list中的a ...
- 201521123073《Java程序设计》第3周学习总结
1. 本周学习总结 2. 书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; p ...
- 32位汇编第六讲,OllyDbg逆向植物大战僵尸,快速定位阳光基址
32位汇编第六讲,OllyDbg逆向植物大战僵尸,快速定位阳光基址 一丶基址,随机基址的理解 首先,全局变量的地址,我们都知道是固定的,是在PE文件中有保存的 但是高版本有了随机基址,那么要怎么解决这 ...