Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:11:41+00:00 2026-06-08T15:11:41+00:00

Ok, I’m trying to use reflection to iterate through a class to get all

  • 0

Ok, I’m trying to use reflection to iterate through a class to get all properties and values of that class and sub classes. The problem I am running into is getting the values of a class that is a subclass of the object I’m working with. For Example. I want to get the ClassRoom properties for the Teacher object in the example below.

Another issue I’m having is how to determine when I get to a containable property such as the List of Students so I can then have it iterate through the list.

ex:

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);                                                    
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:11:44+00:00Added an answer on June 8, 2026 at 3:11 pm
       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);
          }
    
        }
      }
    

    Output

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.