ningshuxia
2022-12-12 e81ca048ef4e9345e904b74ffffd3e8413d18a7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
 
namespace IStation.Extensions
{
    /// <summary>
    /// 枚举拓展
    /// </summary>
    public static class EnumExtension
    {
        /// <summary>
        /// 根据System.ComponentModel.DataAnnotations下的DisplayAttribute特性获取显示文本
        /// </summary>
        public static string GetDisplayText(this Enum t)
        {
            var t_type = t.GetType();
            var fieldName = Enum.GetName(t_type, t);
            var objs = t_type.GetField(fieldName).GetCustomAttributes(typeof(DisplayAttribute), false);
            return objs.Length > 0 ? ((DisplayAttribute)objs[0]).Name : null;
        }
 
 
 
    }
 
}