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. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #12 使用Memory Cgroup限制内存使用量

    HACK #12 使用Memory Cgroup限制内存使用量 Memory Cgroup是Cgroup的资源限制功能之一,可以控制特定进程可以使用的内存量.Memory CgroupMemory C ...

  2. SPM——How to use github

    In this semester, we take a class called 'Software Project Management'. And in this class, we have l ...

  3. Spring cloud Hystrix使用@HystrixCommand使用Hystrix组件及@EnableCircuitBreaker原理介绍

    通过@HystrixCommand注解实现在Spring Cloud使用Hystrix组件相关的工程 cloud-registration-center:注册中心 cloud-service-hyst ...

  4. firemonkey Grid自定义

    http://stackoverflow.com/questions/28893564/memory-leak-on-tstringgrids-ondrawcolumncell-event http: ...

  5. MVC表单提交写法1

    初学MVC,感觉跟以前的aspx页面差别很大,我们就先来看看MVC的表单是怎么提交的吧. 现在我们用一个最简单的例子来看一看MVC是怎么提交表单的(这一个例子中,我们的关注点是如何提交表单,所以不涉及 ...

  6. application获取资源

    通过application获取资源,它的根路径是WebContent,它可以获取web-inf下的资源 通过getclassload()获取资源,它的根路径是classes,不能获取web-inf下的 ...

  7. Gtk-WARNING **: cannot open display: :0.0之解决

    当使用su 到另外一个用户运行某个程序,而这个程序又要有图形显示的时候,就有可能有下面提示: root@dt:~# sudo -i -u keji google-chrome No protocol ...

  8. 131. Palindrome Partitioning (Back-Track, DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. 基于分布式思想下的RPC解决方案--笔记

    分布式: RPC可以提高系统稳定性,比如说,我们的订单服务程序更新出BUG,导致内存溢出,是这台服务器宕机了,但是它只会影响的整个系统的订单业务部分,对于用户注册登录等业务没有影响,同样对于系统的日志 ...

  10. win7 python2.7安装PIL库

    一.前言 遇到客户给了一个需求,需要拼接多个图片,上网找到一个解决方式,不过是需要安装PIL的,相信安装过这个库的应该都遇到很多问题,接下来说说怎么解决. 我的环境是: 操作系统:win10 64bi ...