using System.Windows;
PropertyInfo[] properties = typeof(MyClass).GetProperties();
foreach (var pro in properties)
{
string str = "";
if (pro.GetCustomAttribute<DisplayAttribute>() != null)
{
str = pro.GetCustomAttribute<DisplayAttribute>().Name;
}
else
{
continue;
}
MessageBox.Show(str);
}
class DisplayAttribute : Attribute
{
public DisplayAttribute(string name)
{
this.Name =name;
}
public string Name{get;set;}
}
class MyClass
{
[Display("序号")]
public int Id { get; set; }
[Display("工厂")]
public string Factory { get; set; }
[Display("产线")]
public string Productionline {get;set;}
}
该代码中创建了关于属性名更改的案例,DisplayAttribute该特性是继承与Attribute类的,特性是必须结合反射使用的。