internal static class EnumExt
{
public static List<EnumDefinition> Define<TEnum>() where TEnum : Enum
{
var result = new List<EnumDefinition>();
var keys = Enum.GetNames(typeof(TEnum));
keys.ToList().ForEach(key =>
{
var desc = ((TEnum)Enum.Parse(typeof(TEnum), key))
.GetType()
.GetMember(key)
.FirstOrDefault()
?.GetCustomAttribute<DescriptionAttribute>()
?.Description;
if (string.IsNullOrWhiteSpace(desc))
{
desc = key;
}
var value = (int)(Enum.Parse(typeof(TEnum), key));
var def = EnumDefinition.Create(key, value, desc);
result.Add(def);
});
return result;
}
}
internal class EnumDefinition
{
public string Key { get; private set; }
public int Value { get; private set; }
public string Description { get; private set; }
public static EnumDefinition Create(
string key, int value, string desc)
{
return new EnumDefinition
{
Key = key,
Value = value,
Description = desc
};
}
}