28 lines
931 B
C#
28 lines
931 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
|
|
namespace ENI2.Util
|
|
{
|
|
public static class EnumHelper
|
|
{
|
|
public static string Description (this Enum eValue)
|
|
{
|
|
var nAttributes = eValue.GetType().GetField(eValue.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
if (nAttributes.Any())
|
|
return (nAttributes.First() as DescriptionAttribute).Description;
|
|
return eValue.ToString();
|
|
}
|
|
|
|
public static IEnumerable<KeyValuePair<string, string>> GetAllValuesAndDescription(Type t)
|
|
{
|
|
if (!t.IsEnum)
|
|
throw new ArgumentException("t must be an enum");
|
|
return from e in Enum.GetValues(t).Cast<Enum>()
|
|
select new KeyValuePair<string, string>(e.ToString(), e.Description());
|
|
}
|
|
|
|
}
|
|
}
|