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)对于创建和 ...
随机推荐
- jquery 函数大全
jquery函数大全转载 Attribute:$(”p”).addClass(css中定义的样式类型); 给某个元素添加样式$(”img”).attr({src:”test.jpg”,alt:”te ...
- 交换机的Ethernet Channel
端口聚合也叫做以太通道(ethernet channel),主要用于交换机之间连接.由于两个交换机之间有多条冗余链路的时候,STP会将其中的几条链路关闭,只保留一条,这样可以避免二层的环 路产生.但是 ...
- 新CCIE笔记-路由器的配置
CCIE重修笔记之路由器基本配置与最简单的路由. 路由器与交换机的基本配置命令 全局配置模式下有多种子模式 (华为可以跳跃切换模式) 思科命令行技巧 Tab键补全,也可以直接保留缩写 问号'?'类似l ...
- 获取windows任务栏高度的方法
方法一: TRect rt; SystemParametersInfo(SPI_GETWORKAREA, , &rt, ); //任务栏在下面的高度 int y = ::GetSystemMe ...
- 如何在C++中产生随机数
C++中没有自带的random函数,要实现随机数的生成就需要使用rand()和srand().不过,由于rand()的内部实现是用线性同余法做的,所以生成的并不是真正的随机数,而是在一定范围内可看为随 ...
- FileInputStream 小Demo
要求:设计如下界面 文本框里面可以输入的路径和文件名 单机按钮可以读取在 指定的文件 并把文件内容显示到一个文本域里面来 代码: /** * */ package com.niit.homewo ...
- 微信小程序xml解析
准备: 下载xmldom库:https://github.com/jindw/xmldom 将dom.js.dom-parser.js.sax.js,entities.js拷贝微信小程序需要的文件夹下 ...
- 201521123049 《JAVA程序设计》 第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 作业参考文件下载 1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java ...
- 201521123101 《Java程序设计》第3周学习总结
1. 本周学习总结 2. 书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; p ...
- 201521123028 《Java程序设计》第3周学习总结
1. 本周学习总结 2. 书面作业 Q1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...