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.

5
  • enumDisplayNames is the result you want when get enum's name? Commented Oct 10, 2020 at 2:19
  • yes, I want to stick SourceFromEnum into some function and get out something identical to enumDisplayNames Commented Oct 10, 2020 at 2: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. Commented Oct 10, 2020 at 2:42
  • I agree that post is overly complicated. you can do this in a simple static method using LINQ. Commented Oct 10, 2020 at 2:45
  • Why don't you use a dictionary ? Commented Oct 10, 2020 at 3:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.