【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 ...
随机推荐
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- 开源企业IM免费企业即时通讯ENTBOOST V2014.177版本号正式公布
版权声明:本文为博主原创文章,欢迎转载,转载请尽量保持原文章完整,谢谢! https://blog.csdn.net/yanghz/article/details/30529469 ENTBOOST, ...
- 处理TCP连包的一小段代码
学习网络编程也有一段时间了,一直听说TCP数据会连包,但一直不知道怎么测试好.最近测试了下:发送方使用对列,将发送的数据存入队列,然后开线程,专门发送.发送多包数据之间不延时.在接收方,他们确实连在一 ...
- mybatis中GenericTokenParser的用法
代码示例 import org.apache.ibatis.parsing.GenericTokenParser;import org.apache.ibatis.parsing.TokenHandl ...
- YTU 1055: 输入字符串以及输出
1055: 输入字符串以及输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 694 解决: 476 题目描述 编写一函数,由实参传来一个字符串,统计此字符串中字母.数字.空格和其它字 ...
- Oracle:通过dbv查看数据文件是否有坏块
我们备份的数据文件,可以通过oacle自带的dbv工具来查看是否是好的. 下面实验如下: 环境:oracle10.2.0.1 1.检查数据文件是否有坏块 [oracle@app orcl]$ dbv ...
- RecyclerView 局部刷新(获取viewHolder 去刷新)
RecyclerView.ViewHolder viewHolder = mRecyclerView.findViewHolderForAdapterPosition(i); if (viewHold ...
- Android自动化测试环境搭建
Android自动化环境的搭建主要包括: 1. java jdk和jre的安装和环境的配置 2. appium服务器的安装和配置 3. eclipse开发工具,这里不必要用Android Studio ...
- [Selenium] Grid 介绍
Selenium Grid 支持分布式测试,使测试人员可在分布式环境中进行测试 一般而言,在面临以下情况时可考虑使用Selenium Grid : 1.测试多个浏览器或单个浏览器多个版本,或测试不同操 ...
- MySql必知必会内容导图
<MySQL必知必会>从介绍简单的数据检索开始,逐步深入一些复杂的内容,包括联结的使用.子查询.正则表达式和基于全文本的搜索.存储过程.游标.触发器.表约束,等等.通过重点突出的章节,条理 ...