Bubinga/Thin-Blue-Lie

Simple way to get the Display Names of your enums or the default if none is available

Closed this issue · 1 comments

@Bubing-a Your question was closed on stackoverflow and pointed you to an overly complicated answer, i couldn't provide my answer as the question was closed before i could, so i'm doing it here if it's alright.

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

SourceFromEnum test = new SourceFromEnum();
	
	var me =GetDisplayNames(test);
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;
}

Woah man, you are truly amazing. It works of course, but for you to take the effort to find my repo and answer my question is so kind. Thank you!