Enum Description attribute in dotnet core

24,531

Solution 1

For 1.0 and 1.1, DescriptionAttribute is now in the System.ComponentModel.Primitives NuGet package.

Solution 2

I had to modify @yaniv's answer to use the DescriptionAttribute type and get the Description field.

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;
    }
}

Solution 3

I used this for my Net Framework implementation:

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";
    }
}

This doesn't work for NetCore so I modified it to do this:

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";
    }
}

Enumeration Example:

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

Sample Usage for either static added:

var myDescription = myEnum.Description();

Solution 4

DescriptionAttribute was added to CoreFX, but only after RC2. So it will be there in the RTM version, but not in RC2. Depending on what you want to do, creating your own attribute might work.

Solution 5

You can use "DisplayAttribute" and do

    public static string Description(this Enum enumValue)
    {
        return enumValue.GetType()
                        .GetMember(enumValue.ToString())
                        .First()
                        .GetCustomAttribute<DisplayAttribute>()
                        .GetDescription();
    }
Share:
24,531
TSR
Author by

TSR

Updated on January 21, 2021

Comments

  • TSR
    TSR over 3 years

    Do we have Description attribute for Enums in dot net CLI? (Dot Net Core RC2) If not, any alternative?

  • Adam Tegen
    Adam Tegen over 2 years
    This is still using reflection, yes?
  • noontz
    noontz over 2 years
    @AdamTegen Correct.. FieldInfo and MemberInfo are both in System.Reflection.. My bad!