Skip to content

Instantly share code, notes, and snippets.

@smdooley
Created April 14, 2023 15:39
Show Gist options
  • Save smdooley/288adb404a7abde0f1dc11b3b55c0b40 to your computer and use it in GitHub Desktop.
Save smdooley/288adb404a7abde0f1dc11b3b55c0b40 to your computer and use it in GitHub Desktop.
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace Project.Core.Extensions
{
public static class EnumExtensions
{
public static string GetEnumDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
if (attributes != null && attributes.Any())
{
return attributes.First().Description;
}
return value.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment