EnumHelper.cs 884 B

123456789101112131415161718192021222324252627
  1. using System;
  2. using System.Reflection;
  3. using System.ComponentModel;
  4. namespace SHJX.Service.Common.Utils
  5. {
  6. public static class EnumHelper
  7. {
  8. /// <summary>
  9. /// 获取枚举值上的Description特性的说明
  10. /// </summary>
  11. /// <typeparam name="T">枚举类型</typeparam>
  12. /// <param name="obj">枚举值</param>
  13. /// <returns>特性的说明</returns>
  14. public static string GetEnumDescription<T>(this T obj)
  15. {
  16. var type = obj.GetType();
  17. FieldInfo field = type.GetField(Enum.GetName(type, obj)!);
  18. DescriptionAttribute descAttr = Attribute.GetCustomAttribute(field!, typeof(DescriptionAttribute)) as DescriptionAttribute;
  19. if (descAttr == null)
  20. {
  21. return string.Empty;
  22. }
  23. return descAttr.Description;
  24. }
  25. }
  26. }