Fill all enum value to a List
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace Common.Extension
{
public static class EnumExtension
{
public static IList<object> GetItems(this Type enumType)
{
if (!enumType.IsEnum)
throw new InvalidOperationException();
IList<object> list = new List<object>();
Type typeDescription = typeof(DescriptionAttribute);
FieldInfo[] fields = enumType.GetFields();
string text;
foreach (FieldInfo field in fields)
{
if (!field.FieldType.IsEnum)
continue;
int value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
object[] array = field.GetCustomAttributes(typeDescription, false);
text = array.Length > 0 ? ((DescriptionAttribute)array[0]).Description : field.Name;
list.Add(new { Value = value, Text = text });
}
return list;
}
}
}
Related examples in the same category