我有以下情形:如何通過反射從類中發現基類基類?
public class BaseEntity {
public int Id { get; set; }
}
public class BaseAcademicEntity : BaseEntity { ... }
public class BaseFinancialEntity : BaseEntity { ... }
public class Student : BaseAcademicEntity {
public string Name { get; set; }
public Grade CurrentGrade { get; set; }
}
public class Grade : BaseAcademicEntity {
public string Description { get; set; }
}
好了,現在我會通過反思發現從學生類的屬性。
foreach (PropertyInfo property in typeof(Student).GetProperties()) {
// Here I can discover the type of the current property.
var type = property.PropertyType;
// now, how to discover if this property is from BaseEntity type?
}
就像我在評論中寫的,如何發現屬性是否來自BaseEntity類型?謝謝!
謝謝。它像一個魅力! – Kiwanax