Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EnumExtender.GetEnumValue
public static T GetEnumValue<T>(this string description)
{
var type = typeof(T);
if (!type.GetTypeInfo().IsEnum)
throw new ArgumentException();
var field = type.GetFields().SelectMany(f => f.GetCustomAttributes(typeof(DescriptionAttribute), false), (f, a) => new { Field = f, Att = a })
.Where(a => ((DescriptionAttribute)a.Att).Description == description).SingleOrDefault();
return field == null ? default(T) : (T)field.Field.GetRawConstantValue();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.