枚举

  1. public static class CommonEnums
  2. {
  3. public enum people
  4. {
  5. /// <summary>
  6. ///男人
  7. /// </summary>
  8. [Description("男人")]
  9. man = ,
  10. /// <summary>
  11. /// 女人
  12. /// </summary>
  13. [Description("女人")]
  14. women = ,
  15. }
  16. }

string->枚举

  1. CommonEnums.people en = (CommonEnums.people)Enum.Parse(typeof(CommonEnums.people), "women");

枚举->Dictionary

  1. //使用:生成一个dictionary
  2. Dictionary<int, string> dic=CommonEnums.people.man.ToEnumDictionary();
  3.  
  4. //静态方法
  5. public static class EnumExtension
  6. {
  7. public static Dictionary<int, string> ToEnumDictionary(this Enum enumT, string category = "")
  8. {
  9. Type enumType = enumT.GetType();
  10. if (!enumType.IsEnum) throw new Exception("参数不是枚举");
  11.  
  12. var array = enumType.GetEnumValues();
  13. if (array.Length < ) return new Dictionary<int, string>();
  14. Dictionary<int, string> result = new Dictionary<int, string>(array.Length);
  15.  
  16. int[] enumValues = new int[array.Length];
  17. int index = ;
  18.  
  19. foreach (var item in array)
  20. {
  21. enumValues[index] = Convert.ToInt32(item);
  22. ++index;
  23. }
  24.  
  25. var enumNames = enumType.GetEnumNames();
  26. DescriptionAttribute[] descriptions = null;
  27. CategoryAttribute[] categorys = null;
  28.  
  29. for (index = ; index < array.Length; ++index)
  30. {
  31. FieldInfo fi = enumType.GetField(enumNames[index]);
  32. descriptions = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
  33.  
  34. if (category != string.Empty)
  35. {
  36. categorys = fi.GetCustomAttributes(typeof(CategoryAttribute), false) as CategoryAttribute[];
  37. if (categorys != null && categorys.Length > )
  38. {
  39. if (categorys[].Category != category)
  40. {
  41. continue;
  42. }
  43. }
  44. }
  45.  
  46. if (descriptions != null && descriptions.Length > )
  47. {
  48. result.Add(enumValues[index], descriptions[].Description);
  49. }
  50. else
  51. {
  52. result.Add(enumValues[index], string.Empty);
  53. }
  54. }
  55.  
  56. return result;
  57. }
  58. }

获取枚举描述

  1. //使用
  2. string desc = CommonEnums.people.man.GetDescription();
  3. //方法
  4. public static string GetDescription(this Enum value, bool nameInstend = true)
  5. {
  6. Type type = value.GetType();
  7. string name = Enum.GetName(type, value);
  8. if (name == null)
  9. {
  10. return null;
  11. }
  12. FieldInfo field = type.GetField(name);
  13. DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
  14. if (attribute == null && nameInstend == true)
  15. {
  16. return name;
  17. }
  18. return attribute == null ? null : attribute.Description;
  19. }

 

【C#】枚举的更多相关文章

  1. Swift enum(枚举)使用范例

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  2. 编写高质量代码:改善Java程序的151个建议(第6章:枚举和注解___建议88~92)

    建议88:用枚举实现工厂方法模式更简洁 工厂方法模式(Factory Method Pattern)是" 创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其它子类" ...

  3. Objective-C枚举的几种定义方式与使用

    假设我们需要表示网络连接状态,可以用下列枚举表示: enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateC ...

  4. Help Hanzo (素数筛+区间枚举)

    Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000).     (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼 ...

  5. 枚举:enum

    枚举 所谓枚举就是指定好取值范围,所有内容只能从指定范围取得. 例如,想定义一个color类,他只能有RED,GREEN,BLUE三种植. 使用简单类完成颜色固定取值问题. 1,就是说,一个类只能完成 ...

  6. .NET 基础一步步一幕幕[方法、结构、枚举]

    方法.结构.枚举 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值,写void 方法名:P ...

  7. Asp.Net 将枚举类型(enum)绑定到ListControl(DropDownList)控件

    在开发过程中一些状态的表示使用到枚举类型,那么如何将枚举类型直接绑定到ListControl(DropDownList)是本次的主题,废话不多说了,直接代码: 首先看工具类代码: /// <su ...

  8. 用枚举enum替代int常量

    枚举的好处: 1. 类型安全性 2.使用方便性 public class EnumDemo { enum Color{ RED(3),BLUE(5),BLACK(8),YELLOW(13),GREEN ...

  9. c#编程基础之枚举

    枚举的意义就在于限制变量取值范围. 当可以确定的几种取值时才可以用. 如果输入一个字符串需要进行判断是否是我们需要的字符串时,则一般需要这样写: using System; using System. ...

  10. golang枚举类型 - iota用法拾遗

    在c#.java等高级语言中,经常会用到枚举类型来表示状态等.在golang中并没有枚举类型,如何实现枚举呢?首先从枚举的概念入手. 1.枚举类型定义 从百度百科查询解释如下:http://baike ...

随机推荐

  1. UIBarButtonSystemItem 各种款式

  2. 通过powershell操作eventlog

    relevant command list ~\Desktop> (Get-Command Write-EventLog).Parameters Key Value --- ----- Warn ...

  3. YTU 2391: 求素数

    2391: 求素数 时间限制: 1 Sec  内存限制: 128 MB 提交: 116  解决: 3 题目描述 设计一个程序,输出所有小于等于n(n为一个大于2的正整数)的素数. 要求:(1)每行输出 ...

  4. html5--6-44信纸设计

    html5--6-44信纸设计 实例 <!doctype html> <html> <head> <meta charset="utf-8" ...

  5. codeforces 667B B. Coat of Anticubism(水题)

    题目链接: B. Coat of Anticubism time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. POJ1474:Video Surveillance(求多边形的核)(占位)

    A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- me ...

  7. 「LuoguP1496」 火烧赤壁

    Description 曹操平定北方以后,公元208年,率领大军南下,进攻刘表.他的人马还没有到荆州,刘表已经病死.他的儿子刘琮听到曹军声势浩大,吓破了胆,先派人求降了. 孙权任命周瑜为都督,拨给他三 ...

  8. 洛谷P1290 欧几里德的游戏

    题目:https://www.luogu.org/problemnew/show/P1290 只要出现n>=2*m,就可以每次把较大的数控制在较小的数的一倍与二倍之间,则控制了对方的走法: 每次 ...

  9. 网络爬虫之requests模块的使用+Github自动登入认证

    本篇博客将带领大家梳理爬虫中的requests模块,并结合Github的自动登入验证具体讲解requests模块的参数. 一.引入:   我们先来看如下的例子,初步体验下requests模块的使用: ...

  10. 51nod 1276 【离线化】

    思路1.: 离线处理: 具体就是把岛屿离线然后按照高度排序,把query按照从高到低排序,然后每次query只要从最高的岛屿开始找起,判断条件:如果他旁边都是没有被找过的(也就是默认是海),那么数量+ ...