您的位置:首页 > 编程语言 > C#

C#中读取枚举值的描述属性

2015-11-06 10:29 232 查看
public enum EnumLanugage
{
[System.ComponentModel.Description("中文")]
Chinese,
English
}


public string GetEnumDescription(Enum enumValue)
{
string str = enumValue.ToString();
System.Reflection.FieldInfo field = enumValue.GetType().GetField(str);
object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
if (objs == null || objs.Length == 0) return str;
System.ComponentModel.DescriptionAttribute da = (System.ComponentModel.DescriptionAttribute)objs[0];
return da.Description;
}


转载自:http://www.cnblogs.com/xiaofengfeng/p/4125003.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  enum c#