您的位置:首页 > 其它

获取枚举的title

2015-07-14 15:39 393 查看
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 > 0)
{
output = attrs[0].Value;
}
return output;
}
}


private enum SignMagnitude
{
[StringValue("Negative")]
Negative = -1,
}


使用方法:

StringEnum.GetStringValue(SignMagnitude.Negative);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: