C# 中 Enum使用特殊字符,重写ToString()
public enum EnumTest
{
[Description("等于")]
等=1,
[Description("小于")]
小=2,
[Description("大于")]
大=3
}
public class EnumName
{
public static string GetEnumName(Enum en)
{
Type temType = en.GetType();
MemberInfo[] memberInfos= temType.GetMember(en.ToString());
if (memberInfos != null && memberInfos.Length > 0)
{
object[] objs= memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs != null && objs.Length > 0)
{
return ((DescriptionAttribute)objs[0]).Description;
}
}
return en.ToString();
}
}