首先建立一个枚举:
/// <summary>
/// 蓝色
/// </summary>
Bule,
/// <summary>
/// 绿色
/// </summary>
Green
}
/// <summary>
/// 蓝色
/// </summary>
[Description("蓝色")]
Bule,
/// <summary>
/// 绿色
/// </summary>
[Description("绿色")]
Green
}
/// <summary>
/// 返回指定枚举类型的指定值的描述
/// </summary>
/// <param name="t">枚举类型</param>
/// <param name="v">枚举值</param>
/// <returns></returns>
public static string GetDescription(System.Type t, object v)
{
try
{
FieldInfo oFieldInfo = t.GetField(GetName(t, v));
DescriptionAttribute[] attributes = (DescriptionAttribute[])oFieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
}
catch
{
return "UNKNOWN";
}
}
}
新闻热点
疑难解答