1,获取对象下所有属性的DescriptionAttribute
/// <summary>
/// 获取对象下所有属性的DescriptionAttribute
/// </summary>
/// <param name="o"></param>
/// <param name="note"></param>
/// <returns></returns>
public ArrayList GetPropertyDescriptionAttribute(object o)
{
Type t = o.GetType();//获取实例对象的Type
ArrayList alist = new ArrayList();
foreach (PropertyInfo pi in t.GetProperties())//获取对象的所有属性
{
object[] objs = pi.GetCustomAttributes(typeof(DescriptionAttribute), true);//获取属性的DescriptionAttribute,object为System.ComponentModel.DescriptionAttribute类型
if (objs.Length > 0)
{
string des = ((DescriptionAttribute)objs[0]).Description;//获取DescriptionAttribute的值
alist.Add(des);
}
}
return alist;
}
2,根据属性名获取该属性的DescriptionAttribute
/// <summary>
/// 根据属性名获取该属性的DescriptionAttribute
/// </summary>
/// <param name="o"></param>
/// <param name="Attribute">属性名</param>
/// <returns></returns>
public string GetPropertyDescriptionAttribute(object o,string Attribute)
{
PropertyInfo pinfo = o.GetType().GetProperty(Attribute);
object[] objs = pinfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
if (objs.Length > 0)
{
string des = ((DescriptionAttribute)objs[0]).Description;
return des;
}
else
{
return "";
}
}