首页 > 代码库 > EnumHelper枚举帮助类——反射取得枚举Description

EnumHelper枚举帮助类——反射取得枚举Description

1、需要引用的命名空间: 

1 using System.Collections.Generic;//键值对2 using System.Reflection;//反射3 System.ComponentModel;//指定属性

2、直接上干货,需要的拿走:

(1)、列举所有枚举值和描述

 1         /// <summary> 2         /// 列举所有枚举值和描述 3         /// </summary> 4         /// <typeparam name="T"></typeparam> 5         /// <returns></returns> 6         public static Dictionary<int, string> GetDescriptionDictionary<T>() where T : struct 7         { 8             Type type = typeof(T); 9             Dictionary<int, string> dictionary = new Dictionary<int, string>();10             foreach (int item in System.Enum.GetValues(type))11             {12                 string description = string.Empty;13                 try14                 {15                     FieldInfo fieldInfo = type.GetField(System.Enum.GetName(type, item));16                     if (fieldInfo == null)17                     {18                         continue;19                     }20                     DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute));21                     if (da == null)22                     {23                         continue;24                     }25                     description = da.Description;26                 }27                 catch { }28                 dictionary.Add(item, description);29             }30             return dictionary;31         }

(2)、列举所有枚举值和索引

 1         /// <summary> 2         /// 列举所有枚举值和索引 3         /// </summary> 4         /// <param name="typeParam"></param> 5         /// <returns></returns> 6         public static Dictionary<int, string> EnumToFieldDictionary(Type typeParam) 7         { 8             //Type typeParam = typeof(obj); 9             Dictionary<int, string> dictionary = new Dictionary<int, string>();10             foreach (int i in System.Enum.GetValues(typeParam))11             {12                 string name = System.Enum.GetName(typeParam, i);13                 dictionary.Add(i, name);14             }15             return dictionary;16         }

(3)、获取指定枚举值的描述

 1         /// <summary> 2         /// 获取指定枚举值的描述 3         /// </summary> 4         /// <typeparam name="T"></typeparam> 5         /// <param name="request"></param> 6         /// <returns></returns> 7         public static string GetDescription<T>(T request) where T:struct 8         { 9             try10             {11                 Type type = request.GetType();12                 FieldInfo fieldInfo = type.GetField(request.ToString());13 14                 if (fieldInfo == null) { return string.Empty; }15 16                 DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute));17 18                 if (da != null)19                 {20                     return da.Description;21                 }22                 else23                 {24                     return string.Empty;25                 }26             }27             catch28             {29                 return string.Empty;30             }31         }

(4)、关于扩展
需要的童鞋可以自己写个【获取指定枚举值的描述】的扩展方法,小牛这里没有必要就没写。

 

 【明日小牛】原创——转载请注明出处

EnumHelper枚举帮助类——反射取得枚举Description