ExtensionMethod.NET Home of 828 C#, Visual Basic, F# and Javascript extension methods

Get Enum Description

Gets the description of any Enum value.

Source

public static string GetDescriptionFromEnumValue(Enum value)
{
    DescriptionAttribute attribute = value.GetType()
        .GetField(value.ToString())
        .GetCustomAttributes(typeof(DescriptionAttribute), false)
        .SingleOrDefault() as DescriptionAttribute;
    return attribute == null ? value.ToString() : attribute.Description;
}

Example

var enumDescription= Extensions.GetDescriptionFromEnumValue(CalendarEventDescription.DurationStartDate);

Author: Ufuk Zimmerman

Submitted on: 29 mei 2020

Language: csharp

Type: System.Enum

Views: 3543