EnumExtention.cs 672 B

12345678910111213141516171819202122
  1. using System;
  2. namespace JiaZhiQuan.Common
  3. {
  4. /// <summary>
  5. /// 枚举拓展
  6. /// </summary>
  7. public static class EnumExtention
  8. {
  9. /// <summary>
  10. /// 获取枚举描述
  11. /// </summary>
  12. /// <param name="val"></param>
  13. /// <returns></returns>
  14. public static string GetDescription(this System.Enum val)
  15. {
  16. var field = val.GetType().GetField(val.ToString());
  17. var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
  18. return customAttribute == null ? val.ToString() : ((DescriptionAttribute) customAttribute).Description;
  19. }
  20. }
  21. }