EnumHelper.cs 542 B

123456789101112131415161718
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. namespace ShakerApp.Tools
  5. {
  6. public static class EnumHelper
  7. {
  8. public static string Description<T>(this T e) where T:Enum
  9. {
  10. if (e == null || !e.GetType().IsEnum) return "";
  11. return (e.GetType()?
  12. .GetField(e.ToString())?
  13. .GetCustomAttributes(typeof(DescriptionAttribute), false)
  14. .FirstOrDefault() as DescriptionAttribute)?.Description ?? e.ToString();
  15. }
  16. }
  17. }