EnumExtension

2018/07/03 257

获取Enum的[Description]内容

public static string GetDescription(this Enum value)
{
    if (value != null)
    {
        string name = Enum.GetName(value.GetType(), value);
        if (!string.IsNullOrEmpty(name))
        {
            var field = value.GetType().GetField(name);
            if (!(Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attr))
            {
                return name;
            }
            return attr?.Description;
        }
    }
    return null;
}
评论