C#之枚举的应用--学习笔记
1、枚举定义:
/// <summary>
/// 员工状态
/// </summary>
[Description("员工状态")]
public enum StoreLogType
{
[Description("正常")]
Nomail = 0,
[Description("离职")]
Leave = 1,
[Description("退休")]
Retire = 2,
}
2、获取枚举的描述
public static string GetDescription(Enum e)
{
FieldInfo fi = e.GetType().GetField(e.ToString());
try
{
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes != null && attributes.Length > 0) ? attributes[0].Description : e.ToString();
}
catch
{
return "(未知)";
}
}