获取枚举的title

时间:2023-03-08 20:15:40
 public class StringValue : System.Attribute
{
private readonly string _value; public StringValue(string value)
{
_value = value;
} public string Value
{
get { return _value; }
}
}
 public static class StringEnum
{
public static string GetStringValue(Enum value)
{
string output = null;
Type type = value.GetType();
FieldInfo fi = type.GetField(value.ToString());
StringValue[] attrs =
fi.GetCustomAttributes(typeof (StringValue),
false) as StringValue[];
if (attrs != null && attrs.Length > )
{
output = attrs[].Value;
}
return output;
}
}
private enum SignMagnitude
{
[StringValue("Negative")]
Negative = -,
}

使用方法:

StringEnum.GetStringValue(SignMagnitude.Negative);