public enum infringementCategory
{
[Description("INF0001")]
Infringement,
[Description("INF0002")]
OFN
}
public static class EnumExtension
{
public static string Description(this Enum value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string description = value.ToString();
FieldInfo fieldInfo = value.GetType().GetField(description);
DescriptionAttribute[] attributes =
(DescriptionAttribute[])
fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
description = attributes[0].Description;
}
return description;
}
public static IList<KeyValuePair<Enum, string>> ToList<T>() where T : struct
{
var type = typeof(T);
if (!type.IsEnum)
{
throw new ArgumentException("T must be an enum");
}
return (IList<KeyValuePair<Enum, string>>)
Enum.GetValues(type)
.OfType<Enum>()
.Select(e => new KeyValuePair<Enum, string>(e, e.Description()))
.ToArray();
}
public static T GetValueFromDescription<T>(string description) where T : struct
{
var type = typeof(T);
if(!type.IsEnum)
{
throw new ArgumentException("T must be an enum");
}
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 ArgumentOutOfRangeException("description");
}
}