git_bsmd/ENI2/Util/EnumHelper.cs

30 lines
997 B
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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());
}
}
}