CSharp · Console
using System;
using System.Linq;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
public class Program
{
public static void Main()
{
var names = Enum.GetNames(typeof(Countries)).ToList();
foreach( var name in names )
Console.WriteLine(name);
Console.WriteLine();
names = Enum.GetValues<Countries>().Select(v => v.GetDisplayName()).ToList();
foreach( var name in names )
Console.WriteLine(name);
}
}
public enum Countries
{
Australia,
[Display(Name="New Zealand")]
NewZealand,
}
public static class CountriesExtensions
{
public static string GetDisplayName(this Countries enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
?.GetName() ?? enumValue.ToString();
}
}