1

I created this enum

 public enum Days
    {
        [Display("Monday")]
        Mon,
        [Display("Tuesday")]
        Tue,
        [Display("Wednesday")]
        Wed,
        [Display("Thursday")]
        Thu,
        [Display("Friday")]
        Fri,
        [Display("Satrday")]
        Sat,
    }

I want to get the string "Monday". I tried by doing this

Console.WriteLine(Days.mon);

But is gives me "mon" instead of "Monday". is there any way to get string Monday?

Anas
  • 15
  • 4
  • 1
    Why not use the full name in the Enum itself? Or even better use System.DayOfWeek instead. – Caspar Kleijne Oct 27 '13 at 12:18
  • Does this answer your question? [How to get the Display Name Attribute of an Enum member via MVC Razor code?](https://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code) – Tommehh Jul 19 '23 at 12:17

2 Answers2

11

I've enhanced this answer to give you a more fully rounded-out solution, with better semantic syntax.

using System;
using System.ComponentModel;

public static class EnumExtensions {

    // This extension method is broken out so you can use a similar pattern with 
    // other MetaData elements in the future. This is your base method for each.
    public static T GetAttribute<T>(this Enum value) where T : Attribute {
        var type = value.GetType();
        var memberInfo = type.GetMember(value.ToString());
        var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
        return (T)attributes[0];
    }

    // This method creates a specific call to the above method, requesting the
    // Description MetaData attribute.
    public static string ToName(this Enum value) {
        var attribute = value.GetAttribute<DescriptionAttribute>();
        return attribute == null ? value.ToString() : attribute.Description;
    }

}

This solution creates a pair of extension methods on Enum, to allow you to do what you're looking for. I've enhanced your Enum code slightly, to use the syntax for the DisplayAttribute class of System.ComponentModel.DataAnnotations.

using System.ComponentModel.DataAnnotations;

    public enum Days {
        [Display(Name = "Sunday")]
        Sun,
        [Display(Name = "Monday")]
        Mon,
        [Display(Name = "Tuesday")]
        Tue,
        [Display(Name = "Wednesday")]
        Wed,
        [Display(Name = "Thursday")]
        Thu,
        [Display(Name = "Friday")]
        Fri,
        [Display(Name = "Saturday")]
        Sat
    }

To use the above extension method, you would now simply call the following:

Console.WriteLine(Days.Mon.ToName());

or

var day = Days.Mon;
Console.WriteLine(day.ToName());
Community
  • 1
  • 1
Troy Alford
  • 26,660
  • 10
  • 64
  • 82
  • 1
    Switching `ToName` so it utilizes `var attribute = value.GetAttribute();` requires changing the return to `attribute.Name`, so I at least got that far. What **I'd** like to see is a function that returns some sort of collection (Dictionary, maybe?) that includes key, value pairs for all the items in the `enum` list. Too much to ask? – J.D. Ray Mar 03 '15 at 22:58
  • That's a great idea, J.D. I'd love to see it, if you come up with one. – Troy Alford Mar 06 '15 at 23:15
2
var type = typeof(Days);
var memInfo = type.GetMember(Days.Mon.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(DisplayAttribute),
    false);
var description = ((DisplayAttribute)attributes[0]).Description;

Similar to this solution.

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170