0

I am attempting to get a list of the display names of a given enum. If the enum value has a display name, it should be in the list, if it doesn't the default name should be in the list instead. So if I have an enum like:

 public enum SourceFromEnum
        {
            Youtube,
            Reddit,
            Instagram,
            Facebook,
            Twitter,
            [Display(Name = "News Website")]
            NewsSite,
            [Display(Name = "Phone or Computer")]
            Device,           
        }

the list my function produces should be identical to:

  List<string> enumDisplayNames = new List<string>()
            {
            "Youtube",
            "Reddit",
            "Instagram",
            "Facebook",
            "Twitter",
            "News Website",
            "Phone or Computer"
            };

I have looked at this post, but as far as I can tell the questions are either not providing lists or are overly complicated for what I'm trying to do.

Bubinga
  • 613
  • 12
  • 29
  • enumDisplayNames is the result you want when get enum's name? – VietDD Oct 10 '20 at 02:19
  • yes, I want to stick ```SourceFromEnum``` into some function and get out something identical to ```enumDisplayNames``` – Bubinga Oct 10 '20 at 02:21
  • The post you found tells you exactly what you need. The important part is the `GetDisplayValue()` method (in the accepted answer). You can ignore all the Razor stuff. The other answers provide variations on the same theme, and all generally apply to your scenario. – Peter Duniho Oct 10 '20 at 02:42
  • I agree that post is overly complicated. you can do this in a simple static method using LINQ. – Jonesopolis Oct 10 '20 at 02:45
  • Why don't you use a dictionary ? – Richard Oct 10 '20 at 03:12

2 Answers2

1

Wrote a method quickly, which you can expand from there.

To Use

SourceFromEnum test = new SourceFromEnum();
    
    var me =GetDisplayNames(test);

The Method

public  List<string> GetDisplayNames(Enum enm)
{
    var type=enm.GetType();
    var displaynames = new List<string>();
    var names = Enum.GetNames(type);
    foreach (var name in names)
    {
        var field = type.GetField(name);
        var fds = field.GetCustomAttributes(typeof(DisplayAttribute), true);
        
        if (fds.Length==0)
        {
            displaynames.Add(name);
        }
        
        foreach (DisplayAttribute fd in fds)
        {
            displaynames.Add(fd.Name);
        }
    }
    return displaynames;
}

can make it static,error checking, etc.

Mandivamba
  • 105
  • 1
  • 7
0

I just wrote one, like this:

static List<String> ParseEnums(Type enumType)
{
    if (!enumType.IsEnum || string.IsNullOrEmpty(enumType.FullName))
        return null;

    var ret = new List<string>();
    foreach (var val in Enum.GetValues(enumType))
    {
        var definition = Enum.GetName(enumType, val);
        if (string.IsNullOrEmpty(definition))
            continue;

        // can't use (int)val 
        var code = Convert.ToInt32(val);
        var description = GetDescription(enumType, definition);

        ret.Add(description);
    }

    return ret;
}

static string GetDescription(Type enumType, string field)
{
    FieldInfo fieldInfo = enumType.GetField(field);
    if (fieldInfo == null)
        return string.Empty;

    foreach (var attribute in fieldInfo.GetCustomAttributes())
    {
        if (attribute == null)
            continue;
        if (attribute is DescriptionAttribute descAtt)
            return descAtt.Description;
        else if (attribute.ToString().IndexOf("Display", StringComparison.Ordinal) > 0)
        {
            var prop = attribute.GetType().GetProperty("Name");
            if (prop == null)
                continue;
            return Convert.ToString(prop.GetValue(attribute));
        }
    }

    return null;
}
youbl
  • 134
  • 1
  • 11