using System.ComponentModel; using System.Reflection; namespace FATrace.HKNetLib.Common { public static class EnumExtension { public static string GetDesc(this T em) where T : Enum { Type type = em.GetType(); FieldInfo fd = type.GetField(em.ToString()); var num = Convert.ToInt32(em); if (fd == null) { return $"{num}"; } var firstAttr = fd.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault(); if (firstAttr == null) return $"{num}"; return (firstAttr as DescriptionAttribute).Description; } } }