Last active
May 10, 2024 09:33
-
-
Save GregTrevellick/5d5e2d1dd6ebae87eb69ff79de153b16 to your computer and use it in GitHub Desktop.
C# method to get description for an enum
This file contains hidden or 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; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Reflection; | |
using System.Runtime.CompilerServices; | |
public static T GetEnumFromDescription<T>(string description) | |
{ | |
T result; | |
if (string.IsNullOrEmpty(description)) | |
{ | |
result = default(T); | |
} | |
else | |
{ | |
var type = typeof(T); | |
if (!type.IsEnum) | |
{ | |
throw new ArgumentException(); | |
} | |
var fieldInfos = type.GetFields(); | |
var field = fieldInfos | |
.SelectMany(f => f.GetCustomAttributes(typeof(DescriptionAttribute), false), (f, a) => new { Field = f, Att = a }) | |
.Where(a => ((DescriptionAttribute)a.Att).Description == description).SingleOrDefault(); | |
if (field == null) | |
{ | |
// We did not find a matching description, or some enums just did not have a description perhaps, so see if there is a match on the enum itself | |
var isEnum = EnumTryParse(description, out T theEnum); | |
if (isEnum) | |
{ | |
result = theEnum; | |
} | |
else | |
{ | |
result = default(T); | |
} | |
} | |
else | |
{ | |
result = (T)field.Field.GetRawConstantValue(); | |
} | |
} | |
return result; | |
} | |
private static bool EnumTryParse<T>(string description, out T theEnum) | |
{ | |
foreach (var en in Enum.GetNames(typeof(T))) | |
{ | |
if (en.Equals(description, StringComparison.CurrentCultureIgnoreCase)) | |
{ | |
theEnum = (T)Enum.Parse(typeof(T), description, true); | |
return true; | |
} | |
} | |
theEnum = default(T); | |
return false; | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
using System; | |
using System.ComponentModel; | |
using System.Globalization; | |
namespace Blah1 | |
{ | |
public static class EnumExtensions | |
{ | |
public static string GetDescription<T>(this T e) where T : IConvertible | |
{ | |
string description = null; | |
if (e is Enum) | |
{ | |
var type = e.GetType(); | |
var values = Enum.GetValues(type); | |
foreach (int val in values) | |
{ | |
if (val == e.ToInt32(CultureInfo.InvariantCulture)) | |
{ | |
var memInfo = type.GetMember(type.GetEnumName(val)); | |
var descriptionAttributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); | |
if (descriptionAttributes.Length > 0) | |
{ | |
// Only get first description, any others will be ignored | |
description = ((DescriptionAttribute)descriptionAttributes[0]).Description; | |
} | |
break; | |
} | |
} | |
} | |
return description; | |
} | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
using System; | |
using System.ComponentModel; | |
namespace Blah2 | |
{ | |
public static class EnumExtensions | |
{ | |
public static string GetDescription(this Enum e) | |
{ | |
string description = null; | |
var type = e.GetType(); | |
var values = Enum.GetValues(type); | |
foreach (int val in values) | |
{ | |
var memInfo = type.GetMember(type.GetEnumName(val)); | |
var descriptionAttributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); | |
if (descriptionAttributes.Length > 0) | |
{ | |
// Only get first description, any others will be ignored | |
description = ((DescriptionAttribute)descriptionAttributes[0]).Description; | |
} | |
break; | |
} | |
return description; | |
} | |
} | |
} | |
//Blah3 | |
public static string GetEnumDescription(this Enum value) | |
{ | |
FieldInfo fi = value.GetType().GetField(value.ToString()); | |
DescriptionAttribute[] attributes = | |
(DescriptionAttribute[])fi.GetCustomAttributes( | |
typeof(DescriptionAttribute), | |
false); | |
if (attributes != null && | |
attributes.Length > 0) | |
return attributes[0].Description; | |
else | |
return value.ToString(); | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
using System; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Reflection; | |
namespace Eclipse2.Model.Enums | |
{ | |
public static class EnumHelper | |
{ | |
public static string GetDescription(this Enum value) | |
{ | |
return | |
value | |
.GetType() | |
.GetMember(value.ToString()) | |
.FirstOrDefault() | |
?.GetCustomAttribute<DescriptionAttribute>() | |
?.Description; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment