You can do something like this
for (int i = 0; i < typeof(DepartmentEnum).GetFields().Length - 1; i++) { DepartmentEnum de = EnumExtensions.NumberToEnum<DepartmentEnum>(i); pairs.Add(new KeyValuePair<string, string>(de.ToDescription(), de.ToString())); }
Here is the extension itself:
public static class EnumExtensions { public static string ToDescription(this Enum en) { Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute),false); if (attrs != null && attrs.Length > 0) return ((DescriptionAttribute)attrs[0]).Description; } return en.ToString(); } public static TEnum NumberToEnum<TEnum>(int number ) { return (TEnum)Enum.ToObject(typeof(TEnum), number); } }
epitka
source share