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. PMP Fundamentals

  2. OpenFeign使用笔记

    是什么 Feign是一个声明式Web Service客户端.使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注 ...

  3. 跟我学算法-图像识别之图像分类(下)(GoogleNet网络, ResNet残差网络, ResNext网络, CNN设计准则)

    1.GoogleNet 网络: Inception V1 - Inception V2 - Inception V3 - Inception V4 1. Inception v1 split - me ...

  4. Spring Boot实践——Spring AOP实现之动态代理

    Spring AOP 介绍 AOP的介绍可以查看 Spring Boot实践——AOP实现 与AspectJ的静态代理不同,Spring AOP使用的动态代理,所谓的动态代理就是说AOP框架不会去修改 ...

  5. SublimeText Videos Notes

    [SublimeText Videos Notes] Getting Started 1.Hello:https://tutsplus.com/course/improve-workflow-in-s ...

  6. Android提交自己的作品到GitHub上

    最近在做一个期待上架的我个人写的App,我打算将它开源出去,托管到GitHub上.看了一下网上的教程,其实五花八门,我照着做了,还是没法提交到GitHub上.自己研究了一下,其实非常的简单.这里决定介 ...

  7. linux shell脚本编程笔记(四): 获取字符串长度的七种方法

    获取字符串长度的七种方法 1. \${#str} 2.awk的length 备注:1) 最好用{}来放置变量2) 也可以用length($0)来统计文件中每行的长度 3.awk的NF 备注: -F为分 ...

  8. 22.Generate Parentheses (String; Back-Track)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. 38.Count and Say 报数

    [抄题]: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 1 ...

  10. 用 RPM 打包软件,第 1 部分

    http://www.ibm.com/developerworks/cn/linux/management/package/rpm/part1/ RPM(Red Hat Package Manager ...