publicclassStudent{publicint Id {get;set;}//相当于调用ZDisplayNameAttribute的构造函数[ZDisplayName("学生名称")]publicstring Name {get;set;}//调用ZLenghtRangeAttribute的构造函数[ZLenghtRange(10,20)]publicstring Password {get;set;}}
特性应用
publicstaticvoidPrintInstance(Student student){Type type =typeof(Student);var propertyInfos = type.GetProperties();foreach(PropertyInfo property in propertyInfos){//调用属性的扩展方法 GetCusDisplayName()
Console.WriteLine($"{property.GetCusDisplayName()}:\t{property.GetValue(student)}\t验证结果:{property.LenghtRange(student)}");}}