Created
February 4, 2019 22:18
-
-
Save evgomes/b53d988e71388134a721f4aa5b68fc58 to your computer and use it in GitHub Desktop.
EnumExtensions of Supermarket API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using System.Reflection; | |
namespace Supermarket.API.Extensions | |
{ | |
public static class EnumExtensions | |
{ | |
public static string ToDescriptionString<TEnum>(this TEnum @enum) | |
{ | |
FieldInfo info = @enum.GetType().GetField(@enum.ToString()); | |
var attributes = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false); | |
return attributes?[0].Description ?? @enum.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment