public enum StatusEnum
{
/// <summary>
/// 运行中
/// </summary>
[Description("运行中")]
Running = ,
/// <summary>
/// 已关闭
/// </summary>
[Description("已关闭")]
Stopped =
}
public class EnumHelper
{
#region 静态方法
public static Dictionary<string, string> GetEnumDescription<T>()
{
Dictionary<string, string> dic = new Dictionary<string, string>(); FieldInfo[] fields = typeof(T).GetFields(); foreach (FieldInfo field in fields)
{ if (field.FieldType.IsEnum)
{ object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false); string description = attr.Length == ? field.Name : ((DescriptionAttribute)attr[]).Description; dic.Add(field.Name, description); }
} return dic;
} /// <summary>
/// 获取对应的枚举描述
/// </summary>
public static List<KeyValuePair<string, string>> GetEnumDescriptionList<T>()
{
List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>(); FieldInfo[] fields = typeof(T).GetFields(); foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{ object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false); string description = attr.Length == ? field.Name : ((DescriptionAttribute)attr[]).Description; result.Add(new KeyValuePair<string, string>(field.Name, description)); } }
return result;
} /// <summary>
/// 获取枚举的 值和描述
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<KeyValuePair<int, string>> GetEnumValueDescriptionList<T>()
{
List<KeyValuePair<int, string>> result = new List<KeyValuePair<int, string>>();
FieldInfo[] fields = typeof(T).GetFields();
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{
object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
string description = attr.Length == ? field.Name : ((DescriptionAttribute)attr[]).Description;
result.Add(new KeyValuePair<int, string>(Convert.ToInt32(field.GetValue(null)), description));
}
} return result;
} public static string GetDescriptionByEnumName<T>(string name)
{
try
{
Dictionary<string, string> dic = GetEnumDescription<T>();
string description = dic[name];
return description;
}
catch (Exception ex)
{
return "";
}
}
#endregion
}

C#获取枚举的描述的更多相关文章

  1. .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法

    一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...

  2. 枚举Enum转换为List,获取枚举的描述

    代码: public class EnumberHelper { public static List<EnumberEntity> EnumToList<T>() { Lis ...

  3. C# 获取枚举的描述属性

    在使用枚举类型时,我们需要取名称和值,甚至有时候还需要取枚举类型的描述.通过反射,我们能获取到枚举类型的描述属性. 首先我们需要给枚举类型添加描述属性(属性都没有是不可能取到的),[Descripti ...

  4. C# 获取枚举的描述Description

    方法类: public static class EnumExtensions { #region Enum /// <summary> /// 获取枚举变量值的 Description ...

  5. C#获取枚举描述代码

    public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚 ...

  6. c#枚举 获取枚举键值对、描述等

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...

  7. .net工具类 获取枚举类型的描述

    一般情况我们会用枚举类型来存储一些状态信息,而这些信息有时候需要在前端展示,所以需要展示中文注释描述. 为了方便获取这些信息,就封装了一个枚举扩展类. /// <summary> /// ...

  8. C#记录日志、获取枚举值 等通用函数列表

    )             {                 ] >=  && ipvals[] <=                  && ipval ...

  9. c#枚举的描述和值

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...

随机推荐

  1. django单表操作,增、删、改、查

    一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取出数据. 目的:通过classes(班 ...

  2. DB2 日期时间函数

    db2日期时间函数 (DATE(TRIM(CHAR(DT#11Y))||'-'||TRIM(CHAR(DT#11M))||'-'||TRIM(CHAR(DT#11D))) BETWEEN DATE(' ...

  3. java String标准格式转换Date(yyyy-MM-dd HH:mm:ss)

    SimpleDateFormat sdf=new SimpleDateFormat("yyy年MM月dd日hh点mm分"); Date date=new Date(); Strin ...

  4. 迷你MVVM框架 avalonjs 沉思录 第3节 动态模板

    模板的发明是编程史上的一大里程碑,让我们摆脱了烦锁且易出错的字符串拼接,维护性大大提高. 都在JSP,ASP时代,人们已经学会使用include等语句,将多个页面片断拼接成一个页面. 此外,为了将数据 ...

  5. Redis Key操作

    [Redis Key操作] 1.GETSET key value 将给定 key 的值设为 value ,并返回 key 的旧值(old value). 当 key 存在但不是字符串类型时,返回一个错 ...

  6. Windows NT

    ---------siwuxie095                 Windows NT,全称 Microsoft Windows New Technology     (无关小贴士:NTFS 全 ...

  7. [leetcode]301. Remove Invalid Parentheses 去除无效括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  8. 用Diff和Patch工具维护源码

    在Unix系统下,维护源码版本可以使用很多方法,其中最常用的当然是大名鼎鼎的CVS,但实际上,简单的版本维护工作并没有必要使用复杂的CVS等专门的版本维护工具,Unix标配中的diff和patch工具 ...

  9. cmake 查看配置选项

    cmake 查看配置选项可以用如下命令 cmake . -LH 查看help > cmake -h    cmake version 2.6-patch 4 Usage cmake [optio ...

  10. C语言字符编码处理

    一.字符编码识别 1.简介 uchardet是一个开源的用于文本编码检测的C语言库,其功能模块是用C++实现的,通过一定数量的字符样本独立的分析出文本的编码,当前已经支持UTF-8/GB13080/B ...