受欢迎的博客标签

ASP.NET Core Tag Helper(1)- get the Display Name Attribute and Description Attribute of an Enum

Published

Recently one of my colleague asked me question, he want to create a enum with string values. But C# doesn’t support string based enums, so here is the code snippet which will help you to associate string values to enums. This code is not using any custom attribute, instead it is using DescriptionAttribute class. Here is the enum, with the associated string values.

enum OrderStatus
{
    [Description("New Order")]
    NewOrder = 1,
    [Description("Order is processing")]
    Processing = 2,
    [Description("Order is shipped")]
    Shipped = 3
}

And here is the extension method, which returns the string value associated to the enum

public static class Extensions
{
    public static string Description(this Enum @enum)
    {
        var description = string.Empty;
        var fields = @enum.GetType().GetFields();
        foreach (var field in fields)
        {
            var descriptionAttribute = Attribute.GetCustomAttribute(field,
                typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (descriptionAttribute != null && 
field.Name.Equals(@enum.ToString(), StringComparison.InvariantCultureIgnoreCase))
            {
                description = descriptionAttribute.Description;
                break;
            }
        }

        return description;
    }
}

You can get the description from enum like this.

var orderStatus = OrderStatus.NewOrder;
Console.WriteLine(orderStatus.Description());

normal
 IEnumerable<Stockso.Models.Stock.PanKouDataMenu> values = Enum.GetValues(typeof(Stockso.Models.Stock.PanKouDataMenu)).Cast<Stockso.Models.Stock.PanKouDataMenu>();
            IEnumerable<SelectListItemFilter> items = from value in values
                                                      select new SelectListItemFilter
                                                      {
                                                          Text = value.ToString(),
                                                          Value = ((int)value).ToString(),
                                                      };

Display Name Attribute

  IEnumerable<Stockso.Models.Stock.PanKouDataMenu> values = Enum.GetValues(typeof(Stockso.Models.Stock.PanKouDataMenu)).Cast<Stockso.Models.Stock.PanKouDataMenu>();
            IEnumerable<SelectListItemFilter> items = from value in values
                                                      select new SelectListItemFilter
                                                      {
                                                          Text = value.GetDisplayName().ToString(),
                                                          Value = ((int)value).ToString(),
                                                      };

Description Attribute

 IEnumerable<Stockso.Models.Stock.PanKouDataMenu> values = Enum.GetValues(typeof(Stockso.Models.Stock.PanKouDataMenu)).Cast<Stockso.Models.Stock.PanKouDataMenu>();
            IEnumerable<SelectListItemFilter> items = from value in values
                                                      select new SelectListItemFilter
                                                      {
                                                          Text = value.Description().ToString(),
                                                          Value = ((int)value).ToString(),
                                                      };