わかりました。リフレクションを使用してクラスを反復処理し、そのクラスとサブクラスのすべてのプロパティと値を取得しようとしています。私が遭遇している問題は、作業しているオブジェクトのサブクラスであるクラスの値を取得することです。例えば。次の例で、TeacherオブジェクトのClassRoomプロパティを取得したいと思います。
私が抱えているもう1つの問題は、学生のリストなどの包含可能なプロパティにいつ到達するかを判断して、リストを反復処理できるようにする方法です。
元:
public class Teacher
{
public int ID {get;set;}
public string FullName {get;set;}
public ClassRoom HomeRoom {get;set;}
public List<Student> Students {get;set;}
}
public class ClassRoom
{
public int ID {get;set;}
public string RoomNumber {get;set;}
}
public class Student
{
public int ID {get;set;}
public string FullName {get;set;}
}
private void LoadTeacher()
{
Teacher thisTeacher = Session["Teacher"] as Teacher;
Type type = thisTeacher.GetType();
PropertyInfo[] props = type.GetProperties();
foreach (PropertyInfo prop in props)
{
if (prop.PropertyType.IsClass && prop.PropertyType != typeof(string))
{
Type headerType = prop.PropertyType;
PropertyInfo[] headerProps = headerType.GetProperties();
//Instantiate an object of the headerType
object headerObj = Activator.CreateInstance(headerType);
foreach (PropertyInfo childProp in headerProps)
{
if (!childProp.PropertyType.IsClass || childProp.PropertyType == typeof(string))
{
//This object always get loaded with default values, Why?
object value = childProp.GetValue(headerObj, null);
}
}
}
}
}