【C#】枚举
枚举
public static class CommonEnums
{
public enum people
{
/// <summary>
///男人
/// </summary>
[Description("男人")]
man = ,
/// <summary>
/// 女人
/// </summary>
[Description("女人")]
women = ,
}
}
string->枚举
CommonEnums.people en = (CommonEnums.people)Enum.Parse(typeof(CommonEnums.people), "women");
枚举->Dictionary
//使用:生成一个dictionary
Dictionary<int, string> dic=CommonEnums.people.man.ToEnumDictionary(); //静态方法
public static class EnumExtension
{
public static Dictionary<int, string> ToEnumDictionary(this Enum enumT, string category = "")
{
Type enumType = enumT.GetType();
if (!enumType.IsEnum) throw new Exception("参数不是枚举"); var array = enumType.GetEnumValues();
if (array.Length < ) return new Dictionary<int, string>();
Dictionary<int, string> result = new Dictionary<int, string>(array.Length); int[] enumValues = new int[array.Length];
int index = ; foreach (var item in array)
{
enumValues[index] = Convert.ToInt32(item);
++index;
} var enumNames = enumType.GetEnumNames();
DescriptionAttribute[] descriptions = null;
CategoryAttribute[] categorys = null; for (index = ; index < array.Length; ++index)
{
FieldInfo fi = enumType.GetField(enumNames[index]);
descriptions = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; if (category != string.Empty)
{
categorys = fi.GetCustomAttributes(typeof(CategoryAttribute), false) as CategoryAttribute[];
if (categorys != null && categorys.Length > )
{
if (categorys[].Category != category)
{
continue;
}
}
} if (descriptions != null && descriptions.Length > )
{
result.Add(enumValues[index], descriptions[].Description);
}
else
{
result.Add(enumValues[index], string.Empty);
}
} return result;
}
}
获取枚举描述
//使用
string desc = CommonEnums.people.man.GetDescription();
//方法
public static string GetDescription(this Enum value, bool nameInstend = true)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
}
FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute == null && nameInstend == true)
{
return name;
}
return attribute == null ? null : attribute.Description;
}
【C#】枚举的更多相关文章
- Swift enum(枚举)使用范例
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- 编写高质量代码:改善Java程序的151个建议(第6章:枚举和注解___建议88~92)
建议88:用枚举实现工厂方法模式更简洁 工厂方法模式(Factory Method Pattern)是" 创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其它子类" ...
- Objective-C枚举的几种定义方式与使用
假设我们需要表示网络连接状态,可以用下列枚举表示: enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateC ...
- Help Hanzo (素数筛+区间枚举)
Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000). (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼 ...
- 枚举:enum
枚举 所谓枚举就是指定好取值范围,所有内容只能从指定范围取得. 例如,想定义一个color类,他只能有RED,GREEN,BLUE三种植. 使用简单类完成颜色固定取值问题. 1,就是说,一个类只能完成 ...
- .NET 基础一步步一幕幕[方法、结构、枚举]
方法.结构.枚举 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值,写void 方法名:P ...
- Asp.Net 将枚举类型(enum)绑定到ListControl(DropDownList)控件
在开发过程中一些状态的表示使用到枚举类型,那么如何将枚举类型直接绑定到ListControl(DropDownList)是本次的主题,废话不多说了,直接代码: 首先看工具类代码: /// <su ...
- 用枚举enum替代int常量
枚举的好处: 1. 类型安全性 2.使用方便性 public class EnumDemo { enum Color{ RED(3),BLUE(5),BLACK(8),YELLOW(13),GREEN ...
- c#编程基础之枚举
枚举的意义就在于限制变量取值范围. 当可以确定的几种取值时才可以用. 如果输入一个字符串需要进行判断是否是我们需要的字符串时,则一般需要这样写: using System; using System. ...
- golang枚举类型 - iota用法拾遗
在c#.java等高级语言中,经常会用到枚举类型来表示状态等.在golang中并没有枚举类型,如何实现枚举呢?首先从枚举的概念入手. 1.枚举类型定义 从百度百科查询解释如下:http://baike ...
随机推荐
- Linux时间子系统之四:定时器的引擎:clock_event_device【转】
本文转载自:http://blog.csdn.net/droidphone/article/details/8017604 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] ...
- HDU3567 Eight II —— IDA*算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others) ...
- SQL Server 数据库备份策略,第一周运行失败的原因
一般生产库,采用 每10分钟备份Log,每天备份Diff,每周备份Full的策略. 同时存在异地备份.异地备份可使用SQL Server本身的cmdshell存储过程,调用系统命令. 在为新数据库,建 ...
- MYSQL进阶学习笔记四:MySQL存储过程之定义条件,处理过程及存储过程的管理!(视频序号:进阶_11,12)
知识点五:MySQL存储过程之定义条件和处理过程及存储过程的管理(11,12) 定义条件和处理: 条件的定义和处理可以用来定义在处理过程中遇到的问题时相应的处理步骤. DECLARE CONTINUE ...
- Redis使用基本套路
Redis的数据,通常都是来自于数据库. 存入Redis当中,可以快速的查询.不用每次都关联查询,然后其他处理什么的. 通常可以把一些,不经常变的数据存储其中. 避免数据变动,而Redis缓存数据不变 ...
- JavaScript模板引擎使用
1. [代码]tmpl.js // Simple JavaScript Templating// John Resig - http://ejohn.org/ - MIT Licensed(f ...
- windwo访问linux文件夹方法
windwo访问linux文件夹:是通过linux的samba来实现的: 安装samba需要安装samba-client.samba-common.smaba3个包. 一:安装rpm 现有一个服务器l ...
- 频繁GC会造成卡顿
频繁GC会造成卡顿 https://www.cnblogs.com/qcloud1001/p/9525078.html 一款app除了要有令人惊叹的功能和令人发指交互之外,在性能上也应该追求丝滑的要求 ...
- 微信公众号开发及时获取当前用户Openid及注意事项
目录 (一)微信公众号开发之VS远程调试 (二)微信公众号开发之基础梳理 (三)微信公众号开发之自动消息回复和自定义菜单 (四)微信公众号开发之网页授权获取用户基本信息 (五)微信公众号开发之网页中及 ...
- 【WIP】swift3的timer的用法
创建: 2017/10/14 更新: 2017/10/14 标题加上[WIP],补充创建时间 回家再写