时间:2021-10-27 09:15:43 | 栏目:.NET代码 | 点击:次
首先建立一个枚举:
/// <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";
}
}
}