Skip to content

Instantly share code, notes, and snippets.

@evgomes
Created February 4, 2019 22:18
Show Gist options
  • Save evgomes/b53d988e71388134a721f4aa5b68fc58 to your computer and use it in GitHub Desktop.
Save evgomes/b53d988e71388134a721f4aa5b68fc58 to your computer and use it in GitHub Desktop.
EnumExtensions of Supermarket API
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