zoukankan      html  css  js  c++  java
  • .Net反射-两种方式获取Enum中的值

    public enum EJobType
        {
            客服 = 1, 业务员 = 2, 财务 = 3, 经理 = 4
        }
    Type jobType = typeof(EJobType);

    方式1:

    Array enumItems = Enum.GetValues(jobType);
                foreach (var enumItem in enumItems)
                {
                    int value = (int)enumItem;
                    string text = enumItem.ToString();
                }

    方式2:

    FieldInfo[] fields = jobType.GetFields(BindingFlags.Static | BindingFlags.Public);//
                foreach (var field in fields)
                {
                   string text =  field.Name;
                   object value = field.GetRawConstantValue();
                }

    再分享一个Enum的扩展:

      1 public static class EnumExtensions
      2     {
      3         /// <summary>
      4         /// 枚举信息
      5         /// </summary>
      6         public class EnumInfo<T>
      7         {
      8             /// <summary>
      9             /// 枚举名称
     10             /// </summary>
     11             public string Name { get; set; }
     12             /// <summary>
     13             /// 显示的名称
     14             /// </summary>
     15             public string DisplayName { get; set; }
     16             /// <summary>
     17             /// 18             /// </summary>
     19             public T Value { get; set; }
     20             /// <summary>
     21             /// 描述
     22             /// </summary>
     23             public string Description { get; set; }
     24         }
     25 
     26 
     27         /// <summary>
     28         /// 获取枚举值的名称
     29         /// </summary>
     30         /// <param name="value"></param>
     31         /// <returns></returns>
     32         public static string GetName(this Enum value)
     33         {
     34             return Enum.GetName(value.GetType(), value);
     35         }
     36         /// <summary>
     37         ///  获取该枚举的显示值(如果使用了DisplayAttribute 标签则显示描述中的别名,否则使用 Enum 的名称。)
     38         /// </summary>
     39         /// <param name="value"></param>
     40         /// <returns></returns>
     41         public static string GetText(this Enum value)
     42         {
     43             FieldInfo fi = value.GetType().GetField(value.ToString());
     44             if (fi == null)
     45             {
     46                 return string.Empty;
     47             }
     48 
     49             DisplayAttribute[] attributes = (DisplayAttribute[])fi.GetCustomAttributes(typeof(DisplayAttribute), false);
     50             if ((attributes != null) && (attributes.Length > 0))
     51                 return attributes[0].Name;
     52             else
     53                 return value.ToString();
     54         }
     55 
     56 
     57         /// <summary>
     58         /// 获取枚举代表的值
     59         /// </summary>
     60         /// <param name="value"></param>
     61         /// <returns></returns>
     62         public static int GetValue(this Enum value)
     63         {
     64             return Convert.ToInt32(value);
     65         }
     66         public static T Parse<T>(this Enum enumThis, int value)
     67         {
     68             return (T)Enum.Parse(enumThis.GetType(), value.ToString());
     69         }
     70         public static T Parse<T>(this Enum enumThis, string value)
     71         {
     72             return (T)Enum.Parse(enumThis.GetType(), value);
     73         }
     74         /// <summary>
     75         /// 根据Name获取Value
     76         /// </summary>
     77         /// <param name="value"></param>
     78         /// <returns></returns>
     79         public static int GetEnumValue(Type enumType, string enumName)
     80         {
     81             return (int)Enum.Parse(enumType, enumName);
     82         }
     83         /// <summary>
     84         /// 获取枚举信息(名称,值,描述)
     85         /// </summary>
     86         /// <returns></returns>
     87         public static List<EnumInfo<T>> GetEnumInfo<T>(this Type enumType)
     88         {
     89             if (!enumType.IsEnum) throw new InvalidOperationException("type not a enum!");
     90             List<EnumInfo<T>> emInfos = new List<EnumInfo<T>>();
     91             var fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
     92             foreach (var fi in fields)
     93             {
     94                 EnumInfo<T> emInfo = new EnumInfo<T>();
     95                 emInfo.Name = fi.Name;
     96                 emInfo.Value = (T)fi.GetRawConstantValue();
     97                 var displayAttr = fi.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
     98                 if (displayAttr != null)
     99                 {
    100                     emInfo.DisplayName = ((DisplayAttribute)displayAttr).Name;
    101                     emInfo.Description = ((DisplayAttribute)displayAttr).Description;
    102                 }
    103                 var desAttr = fi.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();
    104                 if (desAttr != null)
    105                 {
    106                     emInfo.Description = ((DescriptionAttribute)desAttr).Description;
    107                 }
    108                 if (string.IsNullOrWhiteSpace(emInfo.Description))
    109                 {
    110                     emInfo.Description = fi.Name;
    111                 }
    112                 if (string.IsNullOrWhiteSpace(emInfo.DisplayName))
    113                 {
    114                     emInfo.DisplayName = fi.Name;
    115                 }
    116                 emInfos.Add(emInfo);
    117             }
    118             return emInfos;
    119         }
    120         public static List<EnumInfo<object>> GetEnumInfo(this Type enumType)
    121         {
    122             return GetEnumInfo<object>(enumType);
    123         }
    124     }
  • 相关阅读:
    分析现有 WPF / Windows Forms 程序能否顺利迁移到 .NET Core 3.0(使用 .NET Core 3.0 Desktop API Analyzer )
    .NET 命令行参数包含应用程序路径吗?
    WPF 多线程 UI:设计一个异步加载 UI 的容器
    .NET/C# 使用反射调用含 ref 或 out 参数的方法
    .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
    .NET/C# 使用反射注册事件
    都是用 DllImport?有没有考虑过自己写一个 extern 方法?
    好的框架需要好的 API 设计 —— API 设计的六个原则
    Netty源码学习(七)FastThreadLocal
    Netty源码学习(六)ChannelPipeline
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/11974884.html
Copyright © 2011-2022 走看看