枚举的绑定
1、定义枚举
public enum EnumTest
{
[Description("开始")]
开始 = 1,
[Description("结束")]
结束 = 2,
[Description("运行中")]
运行中 = 3
}
2、扩展函数
引入命名空间
using System.ComponentModel;
using System.Reflection;
public static string GetEnumDesc(this Enum enumName)
{
Type type = enumName.GetType();
MemberInfo[] meminfo = type.GetMember(enumName.ToString());
if (meminfo != null && meminfo.Length > 0)
{
object[] obj = meminfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
if (obj != null && obj.Length > 0)
{
return ((DescriptionAttribute)obj[0]).Description;
}
}
return enumName.ToString();
}
调用方式:EnumTest.开始.GetEnumDesc();