【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 ...
随机推荐
- ElasticSearch远程随意代码运行漏洞(CVE-2014-3120)分析
原理 这个漏洞实际上非常easy,ElasticSearch有脚本运行(scripting)的功能,能够非常方便地对查询出来的数据再加工处理. ElasticSearch用的脚本引擎是MVEL,这个引 ...
- Aspose 直接插入SQL Server DataTalbe
原文链接:http://www.cnblogs.com/hellohongfu/p/7362830.html 下面的代码可以根据excel文件,生成创建表的SQL,以及测试InsertSQL .方法将 ...
- spring boot 使用Ehcache
1-引入maven依赖: 2-增加ehcache.xml 3-bootstrap.yml配置ehcache.xml的路径 4-启动类加注解@EnableCaching 5-使用处加注解@Cacheab ...
- date 命令 时间戳到标准格式转换
1. 知道时间戳看标准时间, 时间戳到 秒: Wed Apr :: CST 2. 看到前时间时间戳格式 date +%s 3. 知道某个标准时间, 看时间戳 date -d "Wed Apr ...
- Linux 进程等待队列【转】
本文转载自:http://blog.csdn.net/dlutbrucezhang/article/details/9212067 Linux内核的等待队列是以双循环链表为基础数据结构,与进程调度机制 ...
- bzoj5328: [Sdoi2018]物理实验
果然我还是太菜了,爆了一天才过....隔壁肉丝都不知道喊了多少句哎╮(╯▽╰)╭我又A了什么傻逼题(然鹅就是wf和国集的题QWQ) 其实这个题就是个裸题,但是我就是不会... 这个题第一步就是明显的旋 ...
- MYSQL初级学习笔记五:连接查询!(视频序号:初级_37-41)
知识点七:连接查询(37-41) 什么是连接查询: 连接查询是将两个或两个以上的表按某个条件连接起来,从中选取需要的数据.连接查询是同时查询两个或两个以上的表时使用的.当不同的表中存在相同意义的字段时 ...
- 一步一步学Silverlight 2系列(30):使用Transform实现更炫的效果(下)
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- module+standard library.py
#导入模块 import sys sys.path sys.path.append('D:\program files\Python34\PyWorks') #hello.py文件路径 #不用appe ...
- BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP
BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP Description 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园. ...