c# GetDescription

    public static string? GetDescription(this Enum value, bool nameInstead = true)
    {
        Type type = value.GetType();
        string? name = Enum.GetName(type, value);
        if (name == null)return null;
        FieldInfo ? field = type.GetField(name);
        if (field == null) return null;
        DescriptionAttribute? attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
        if (attribute == null && nameInstead) return name;
        return attribute?.Description;
    }
posted @ 2022-05-31 23:19  月渊  阅读(191)  评论(0)    收藏  举报