通过枚举值获取枚举属性文本
时间:2011-03-15 来源:杨永辉
public static string GetEnumOptText(Type type, int value)
{
FieldInfo[] fields = type.GetFields();
for (int i = 1, count = fields.Length; i < count; i++)
{
FieldInfo field = fields[i];
if (((int)Enum.Parse(type, field.Name)).ToString() == value.ToString())
{
object[] objs = field.GetCustomAttributes(typeof(EnumTextValueAttribute), false);
if (objs == null || objs.Length == 0)
{
return field.Name;
}
else
{
EnumTextValueAttribute da = (EnumTextValueAttribute)objs[0];
return da.Text;
}
}
}
return "";
}
相关阅读 更多 +