I have an emun defined as in the sample below.
public enum SampleEnum : int
{
[Display(Name = "One")]
Test_One = 1,
[Display(Name = "Two")]
Test_Two = 2,
[Display(Name = "Three")]
Test_Three = 3
}
In the below line of code, how can get the Display instead name?
var displayName = Enum.GetName(typeof(SampleEnum ), 2);
In the above line, i would like to get Two instead of Test_Two
You can create Extension method for the same.
public static class EnumExtensions
{
public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
?.GetName();
}
}
https://dnilvincent.com/blog/posts/how-to-get-enum-display-name-in-csharp-net
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With