Fransic

导航

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 "(未知)";
}
}

posted on 2011-04-22 13:48  Fransic  阅读(258)  评论(0编辑  收藏  举报