12345678910111213141516171819202122232425262728293031323334 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- /// <summary>
- /// 枚举扩展
- /// </summary>
- public static class EnumExtension
- {
- /// <summary>
- /// 获取枚举描述
- /// </summary>
- /// <param name="enumType">枚举类型</param>
- /// <returns></returns>
- public static string GetEnumDisplayName(this Enum enumType)
- {
- try
- {
- Type type = enumType.GetType();
- var field = type.GetField(Enum.GetName(type, enumType));
- string displayName = field.GetCustomAttribute<DisplayAttribute>().Name;
- return displayName;
- }
- catch
- {
- return string.Empty;
- }
- }
- }
|