public static class GetDescription
{
/// <summary>
/// 获取描述信息
/// </summary>
/// <param name="en"></param>
/// <returns></returns>
public static string description(this Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
return ((DescriptionAttribute)attrs[0]).Description;
}
return en.ToString();
}
}
public enum Sex
{
[Description("男")]
man = 1,
[Description("女")]
woman = 2,
[Description("其他")]
other = 3
}
本文转自51GT51CTO博客,原文链接: http://blog.51cto.com/yataigp/1951005,如需转载请自行联系原作者