0

わかりました。リフレクションを使用してクラスを反復処理し、そのクラスとサブクラスのすべてのプロパティと値を取得しようとしています。私が遭遇している問題は、作業しているオブジェクトのサブクラスであるクラスの値を取得することです。例えば。次の例で、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);                                                    
                }
            }
        }
    }
}
4

1 に答える 1

4
   var teacher = new Teacher()
    {
      HomeRoom = new ClassRoom { ID = 12, RoomNumber = "7-1" },
      Students = new List<Student> { new Student{ID =1, FullName = "Smith1"}, new Student{ID=2, FullName = "Smith2"} }
    };
    Console.WriteLine("Teacher");
    Browse(teacher, 1);

 static void Browse(object item, int level = 0)
  {
    if (item == null)
      return;
    var prefix = new string(' ', level * 2);

    var type = item.GetType();
    foreach (var prop in type.GetProperties())
    {
      var value = prop.GetValue(item, null);
      if (value is System.Collections.IEnumerable && !(value is string))
      {
        Console.WriteLine("{0}{1}", prefix, prop.Name);
        foreach (var index_entry in ((System.Collections.IEnumerable)value).OfType<object>().Select((entry, index) => new { entry, index }))
        {
          Console.WriteLine("{0}[{1}]: {2}", prefix, index_entry.index, index_entry.entry);
          Browse(index_entry.entry, level + 1);
        }
      }
      else if (value != null && !value.GetType().IsPrimitive && !(value is string))
      {
        Console.WriteLine("{0}{1}: {2}", prefix, prop.Name, value);
        Browse(value, level + 1);
      }
      else
      {
        Console.WriteLine("{0}{1}: {2}", prefix, prop.Name, value);
      }

    }
  }

出力

Teacher
  ID: 0
  FullName:
  HomeRoom: Program+ClassRoom
    ID: 12
    RoomNumber: 7-1
  Students
  [0]: Program+Student
    ID: 1
    FullName: Smith1
  [1]: Program+Student
    ID: 2
    FullName: Smith2
于 2012-07-26T23:29:19.893 に答える