在使用处引入包using System.ComponentModel;
在类成员前添加特性,如
public class A{
[Description("姓名")]
public string name {get;set;}
}
使用时
//获取类A的元数据数组
System.Reflection.PropertyInfo[] propertyInfos = typeof(A).GetProperties()
foreach(var item in propertyInfos ){
//获取成员描述
DescriptionAttribute customAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute));
//name值为姓名
string name = customAttribute.Description;
}