using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StandardLibrary { public static class EnumExt { public static bool TryParseEnum(object value, out T result) where T : struct, Enum { result = default; if (Enum.IsDefined(typeof(T), value)) { result = (T)Enum.ToObject(typeof(T), value); return true; } return false; } /// /// 获取枚举值的 Description 特性文本,如果没有则返回枚举名称 /// public static string GetDescription(this Enum value) { var field = value.GetType().GetField(value.ToString()); if (field == null) return value.ToString(); var attribute = Attribute.GetCustomAttribute( field, typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute?.Description ?? value.ToString(); } } }