using System;
using System.ComponentModel;
namespace ZeroLevel
{
public static class EnumExtensions
{
///
/// Gets an attribute on an enum field value
///
/// The type of the attribute you want to retrieve
/// The enum value
/// The attribute of type T that exists on the enum value
/// string desc = myEnumVariable.GetAttributeOfType().Description;
public static T GetAttributeOfType(this Enum enumVal)
where T : Attribute
{
var type = enumVal.GetType();
var memInfo = type.GetMember(enumVal.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
return (attributes.Length > 0) ? (T)attributes[0] : null;
}
public static string Description(this Enum enumVal)
{
var attr = enumVal.GetAttributeOfType();
return attr?.Description;
}
public static T GetValueFromDescription(string description)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}
throw new ArgumentException("Not found.", nameof(description));
// or return default(T);
}
}
}