zoukankan      html  css  js  c++  java
  • 枚举转化成List及读取枚举Description属性值

     1   public static class EnumHelper
     2     {
     3         /// <summary>
     4         /// 获取枚举变量值的 Description 属性
     5         /// </summary>
     6         /// <param name="obj">枚举变量</param>
     7         /// <param name="isTop">是否改变为返回该类、枚举类型的头 Description 属性,而不是当前的属性或枚举变量值的 Description 属性</param>
     8         /// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns>
     9         public static string GetDescription(this object obj, bool isTop)
    10         {
    11             if (obj == null)
    12             {
    13                 return string.Empty;
    14             }
    15             try
    16             {
    17                 Type _enumType = obj.GetType();
    18                 DescriptionAttribute dna = null;
    19                 if (isTop)
    20                 {
    21                     dna = (DescriptionAttribute)Attribute.GetCustomAttribute(_enumType, typeof(DescriptionAttribute));
    22                 }
    23                 else
    24                 {
    25                     FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, obj));
    26                     dna = (DescriptionAttribute)Attribute.GetCustomAttribute(
    27                        fi, typeof(DescriptionAttribute));
    28                 }
    29                 if (dna != null && string.IsNullOrEmpty(dna.Description) == false)
    30                     return dna.Description;
    31             }
    32             catch
    33             {
    34             }
    35             return obj.ToString();
    36         }
    37 
    38         /// <summary>
    39         /// 将枚举转成List
    40         /// </summary>
    41         /// <typeparam name="T"></typeparam>
    42         /// <returns></returns>
    43         public static List<EnumberEntity> EnumToList<T>()
    44         {
    45             List<EnumberEntity> list = new List<EnumberEntity>();
    46 
    47             foreach (var e in Enum.GetValues(typeof(T)))
    48             {
    49                 EnumberEntity m = new EnumberEntity();
    50                 object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
    51                 if (objArr != null && objArr.Length > 0)
    52                 {
    53                     DescriptionAttribute da = objArr[0] as DescriptionAttribute;
    54                     m.Desction = da.Description;
    55                 }
    56                 m.EnumValue = Convert.ToInt32(e);
    57                 m.EnumName = e.ToString();
    58                 list.Add(m);
    59             }
    60             return list;
    61         }
    62     }
    63 
    64     public class EnumberEntity
    65     {
    66         /// <summary>
    67         /// 枚举的描述
    68         /// </summary>
    69         public string Desction { set; get; }
    70 
    71         /// <summary>
    72         /// 枚举名称
    73         /// </summary>
    74         public string EnumName { set; get; }
    75 
    76         /// <summary>
    77         /// 枚举对象的值
    78         /// </summary>
    79         public int EnumValue { set; get; }
    80 
    81     }

    定义枚举:

     1  /// <summary>
     2     /// 操作类型
     3     /// d:2019-12-4
     4     /// </summary>
     5     public enum OperateType
     6     {
     7         /// <summary>
     8         /// 增加
     9         /// </summary>
    10         [Description("增加")]
    11         Add = 1,
    12 
    13         /// <summary>
    14         /// 修改
    15         /// </summary>
    16         [Description("修改")]
    17         Edit = 2,
    18 
    19         /// <summary>
    20         /// 删除
    21         /// </summary>
    22         [Description("删除")]
    23         Del = 3
    24 
    25         ///// <summary>
    26         ///// 查询
    27         ///// </summary>
    28         //[Description("查询")]
    29         //Query = 4
    30     }
  • 相关阅读:
    终于有人把MYSQL索引讲清楚了
    计算机基础知识总结与操作系统 PDF 下载
    java电子书python电子书等PDF下载方式
    redis布隆过滤器
    IntelliJ IDEA 2020.1 激活教程,亲测可用
    Another Redis DeskTop Manager一款稳定全新的redis连接工具
    一文吃透redis持久化,妈妈再也不担心我面试过不了!
    ES6 运算符
    If-Else的5种方法从入门到高级示例
    ES6中 的类(class)
  • 原文地址:https://www.cnblogs.com/nayilvyangguang/p/11988917.html
Copyright © 2011-2022 走看看