dotnet 核心中的枚举描述属性

Enum Description attribute in dotnet core

.net CLI 中的枚举是否有 Description 属性? (点网核心 RC2) 如果没有,还有其他选择吗?

DescriptionAttribute was added to CoreFX,但仅在 RC2 之后。所以它会出现在 RTM 版本中,但不会出现在 RC2 中。根据您想要执行的操作,创建您自己的属性可能会奏效。

我将其用于我的 Net Framework 实现:

public static class EnumerationExtension
{
    public static string Description( this Enum value )
    {
        // get attributes  
        var field = value.GetType().GetField( value.ToString() );
        var attributes = field.GetCustomAttributes( typeof( DescriptionAttribute ), false );

        // return description
        return attributes.Any() ? ( (DescriptionAttribute)attributes.ElementAt( 0 ) ).Description : "Description Not Found";
    }
}

这对 NetCore 不起作用,所以我对其进行了修改:

public static class EnumerationExtension
{
    public static string Description( this Enum value )
    {
        // get attributes  
        var field = value.GetType().GetField( value.ToString() );
        var attributes = field.GetCustomAttributes( false );

        // Description is in a hidden Attribute class called DisplayAttribute
        // Not to be confused with DisplayNameAttribute
        dynamic displayAttribute = null;

        if (attributes.Any())
        {
            displayAttribute = attributes.ElementAt( 0 );
        }

        // return description
        return displayAttribute?.Description ?? "Description Not Found";
    }
}

枚举示例:

public enum ExportTypes
{
    [Display( Name = "csv", Description = "text/csv" )]
    CSV = 0
}

添加的任一静态示例用法:

var myDescription = myEnum.Description();

对于 1.0 和 1.1,DescriptionAttribute is now in the System.ComponentModel.Primitives NuGet 包。

您可以使用 "DisplayAttribute" 并执行

    public static string Description(this Enum enumValue)
    {
        return enumValue.GetType()
                        .GetMember(enumValue.ToString())
                        .First()
                        .GetCustomAttribute<DisplayAttribute>()
                        .GetDescription();
    }

我不得不修改@yaniv 的答案以使用 DescriptionAttribute 类型并获取 Description 字段。

public static class EnumExtensions
{
    /// <summary>
    /// Get the Description from the DescriptionAttribute.
    /// </summary>
    /// <param name="enumValue"></param>
    /// <returns></returns>
    public static string GetDescription(this Enum enumValue)
    {
        return enumValue.GetType()
                   .GetMember(enumValue.ToString())
                   .First()
                   .GetCustomAttribute<DescriptionAttribute>()?
                   .Description ?? string.Empty;
    }
}

为了避免按照@pamcevoj 的回答使用System.Reflection 并按照@HouseCat 的回答使用动态,我在基于@HouseCat 的解决方案构建的.NET Core 3.1 中找到了这个解决方案

public static string Description(this Enum enumValue)
    {
        var descriptionAttribute = enumValue.GetType()
            .GetField(enumValue.ToString())
            .GetCustomAttributes(false)
            .SingleOrDefault(attr => attr.GetType() == typeof(DescriptionAttribute)) as DescriptionAttribute;

        // return description
        return descriptionAttribute?.Description ?? "";
    }