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

C# 获取属性字段上DescriptionAttribute的值

2014-05-20 10:51 357 查看
var ent = new Ent();
foreach (var item in ent.GetType().GetProperties())
{
var v = (DescriptionAttribute[])item.GetCustomAttributes(typeof(DescriptionAttribute), false);
var descriptionName = v[0].Description;

item.SetValue(ent,descriptionName+":1");
}


private class EnumHelper
{
/// <summary>
/// 获取枚举值上的Description特性的说明
/// </summary>
/// <typeparam name="T">枚举类型</typeparam>
/// <param name="obj">枚举值</param>
/// <returns>特性的说明</returns>
public static string GetEnumDescription<T>(T obj)
{
var type = obj.GetType();
FieldInfo field = type.GetField(Enum.GetName(type, obj));
DescriptionAttribute descAttr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (descAttr == null)
{
return string.Empty;
}

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