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 6799495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:51:19+00:00 2026-05-26T18:51:19+00:00

I’m attempting to create a recursive routine that will retrieve PropertyInfos for all members

  • 0

I’m attempting to create a recursive routine that will retrieve PropertyInfos for all members under a specified object (in .NET 3.5). Everything for immediate members is working, but it needs to parse nested classes as well (and their nested classes, etc).

I do not understand how to handle the section that parses nested classes. How would you write this part of the code?

public class ObjectWalkerEntity
{
    public object Value { get; set; }
    public PropertyInfo PropertyInfo { get; set; }
}


public static class ObjectWalker
{
    // This will be the returned object
    static List<ObjectWalkerEntity> objectList = new List<ObjectWalkerEntity>();

    public static List<ObjectWalkerEntity> Walk(object o)
    {
        objectList.Clear();
        processObject(o);
        return objectList;
    }

    private static void processObject(object o)
    {
        if (o == null)
        {
            return;
        }

        Type t = o.GetType();

        foreach (PropertyInfo pi in t.GetProperties())
        {
            if (isGeneric(pi.PropertyType))
            {
                // Add generic object
                ObjectWalkerEntity obj = new ObjectWalkerEntity();
                obj.PropertyInfo = pi;
                obj.Value = pi.GetValue(o, null);
                objectList.Add(obj);
            }
            else
            {
                ////// TODO: Find a way to parse the members of the subclass...
                // Parse each member of the non-generic object
                foreach (Object item in pi.PropertyType)
                {
                    processObject(item);
                }
            }
        }

        return;
    }

    private static bool isGeneric(Type type)
    {
        return
            Extensions.IsSubclassOfRawGeneric(type, typeof(bool)) ||
            Extensions.IsSubclassOfRawGeneric(type, typeof(string)) ||
            Extensions.IsSubclassOfRawGeneric(type, typeof(int)) ||
            Extensions.IsSubclassOfRawGeneric(type, typeof(UInt16)) ||
            Extensions.IsSubclassOfRawGeneric(type, typeof(UInt32)) ||
            Extensions.IsSubclassOfRawGeneric(type, typeof(UInt64)) ||
            Extensions.IsSubclassOfRawGeneric(type, typeof(DateTime));
    }

Edit: I’ve used some of Harlam’s suggestions, and came up with a working solution. This handles both nested classes and lists.

I’ve replaced my previous loop through the propertyinfo with the following

foreach (PropertyInfo pi in t.GetProperties())
{
    if (isGeneric(pi.PropertyType))
    {
        // Add generic object
        ObjectWalkerEntity obj = new ObjectWalkerEntity();
        obj.PropertyInfo = pi;
        obj.Value = pi.GetValue(o, null);
        objectList.Add(obj);
    }
    else if (isList(pi.PropertyType))
    {
        // Parse the list
        var list = (IList)pi.GetValue(o, null);
        foreach (object item in list)
        {
            processObject(item);
        }
    }
    else
    {
        // Parse each member of the non-generic object
        object value = pi.GetValue(o, null);
        processObject(value);
    }
}

I’ve also added a new check to see if something is a list.

private static bool isList(Type type)
{
    return
        IsSubclassOfRawGeneric(type, typeof(List<>));
}

Thanks for the help!

  • 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-05-26T18:51:19+00:00Added an answer on May 26, 2026 at 6:51 pm

    I think this will work for you. The idea here is to return an enumerable object from each call to ProcessObject() and then roll those calls up into the callers List<ObjectWalkerEntity>.

    public class ObjectWalkerEntity
    {
       public object Value { get; set; }
       public PropertyInfo PropertyInfo { get; set; }
    }
    
    public static class ObjectWalker
    {
       public static List<ObjectWalkerEntity> Walk(object o)
       {
          return ProcessObject(o).ToList();
       }
    
       private static IEnumerable<ObjectWalkerEntity> ProcessObject(object o)
       {
          if (o == null)
          {
             // nothing here, just return an empty enumerable object
             return new ObjectWalkerEntity[0];
          }
    
          // create the list to hold values found in this object
          var objectList = new List<ObjectWalkerEntity>();
    
          Type t = o.GetType();
          foreach (PropertyInfo pi in t.GetProperties())
          {
             if (IsGeneric(pi.PropertyType))
             {
                // Add generic object
                var obj = new ObjectWalkerEntity();
                obj.PropertyInfo = pi;
                obj.Value = pi.GetValue(o, null);
                objectList.Add(obj);
             }
             else
             {
                // not generic, get the property value and make the recursive call
                object value = pi.GetValue(o, null);
                // all values returned from the recursive call get 
                // rolled up into the list created in this call.
                objectList.AddRange(ProcessObject(value));
             }
          } 
    
          return objectList.AsReadOnly();
       }
    
       private static bool IsGeneric(Type type)
       {
          return
              IsSubclassOfRawGeneric(type, typeof(bool)) ||
              IsSubclassOfRawGeneric(type, typeof(string)) ||
              IsSubclassOfRawGeneric(type, typeof(int)) ||
              IsSubclassOfRawGeneric(type, typeof(UInt16)) ||
              IsSubclassOfRawGeneric(type, typeof(UInt32)) ||
              IsSubclassOfRawGeneric(type, typeof(UInt64)) ||
              IsSubclassOfRawGeneric(type, typeof(DateTime));
       }
    
       private static bool IsSubclassOfRawGeneric(Type generic, Type toCheck)
       {
          while (toCheck != typeof(object))
          {
             var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
             if (generic == cur)
             {
                return true;
             }
             toCheck = toCheck.BaseType;
          }
          return false;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
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
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string

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.