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

  • Home
  • SEARCH
  • 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 6993769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:48:24+00:00 2026-05-27T19:48:24+00:00

I got a Big hierarchic class that i would like to loop all it’s

  • 0

I got a Big hierarchic class that i would like to loop all it’s properties and sub properties etc..

Example

public class RootClass
{
   // properties ..
   List<Item> FirstItemsList { get; set;}
   List<Item> SecondItemsList { get; set;}
   SubClass SubClass { get; set;}
}
public class SubClass
{
   // properties ..
   List<Item> ThirdItemsList { get; set;}
}
public class Item
{
   //properties 
}

i want a function that will return me a list of all Item type found
i.e

public IList<Item> GetAllItemsInClass(RootClass entity);

Thanks

  • 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-27T19:48:24+00:00Added an answer on May 27, 2026 at 7:48 pm

    If you need something that would work in a general case (any class hierarchy) then you could do the following:

    You will need a recursive algorithm (function). The algorithm would loop over the members, adding their types to a list (if they’re not already added) and then returning that list, COMBINED with the types of the members of the type just added to the list-here you make the recursive call. The terminating conditions would be:

    1. The type of a member is a primitive (to check this use Type.IsPrimitive).
    2. The type being reflected is defined in another assembly. You can check the defining assembly using Type.Assembly.

    If you need something simpler, then use the technique above but just use an if statement in the loop.

    Let me know if this is what you want or no and then I will post a code sample for you when I have more time.

    Update: The following is a code sample showing how you can process a type and recursively get all the types contained within it. You can call it this way: List typesHere = GetTypes(myObject.GetType())

        public static List<Type> GetTypes(Type t)
        {
            List<Type> list = new List<Type>();
            if (t.IsPrimitive)
            {
                if (!list.Contains(t))
                    list.Add(t);
                return list;
            }
            else if (!t.Assembly.Equals(System.Reflection.Assembly.GetExecutingAssembly()))
            {
                //if the type is defined in another assembly then we will check its
                //generic parameters. This handles the List<Item> case.
                var genArgs = t.GetGenericArguments();
                if (genArgs != null)
                    foreach (Type genericArgumentType in genArgs)
                    {
                        if(!list.Contains(genericArgumentType))
                            list.AddRange(GetTypes(genericArgumentType));
                    }
                return list;
            }
            else
            {
                //get types of props and gen args
                var types = t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Select(pi => pi.PropertyType).ToList();
                types.AddRange(t.GetGenericArguments());
                foreach (System.Type innerType in types)
                {
                    //get the object represented by the property to traverse the types in it.
                    if (!list.Contains(innerType))
                        list.Add(innerType);
                    else continue; //because the type has been already added and as thus its child types also has been already added.
    
                    var innerInnerTypes = GetTypes(innerType);
                    //add the types filtering duplicates
                    foreach (Type t1 in innerInnerTypes) //list.AddRange(innerTypes); //without filtering duplicates.
                        if (!list.Contains(t1))
                            list.Add(t1);
                }
                return list;
            }
        }
    

    So when I ran this on the classes you posted in your original post (Item having two primitive properties like below) I got the following list:

    GetTypes(typeof(List<Item>))
    Count = 3
        [0]: {Name = "Item" FullName = "AssemblyNameXYZ.Item"}
        [1]: {Name = "String" FullName = "System.String"}
        [2]: {Name = "Int32" FullName = "System.Int32"}
    
    GetTypes(typeof(Item))
    Count = 2
        [0]: {Name = "String" FullName = "System.String"}
        [1]: {Name = "Int32" FullName = "System.Int32"}
    
    Reflection.GetTypes(typeof(RootClass))
    Count = 5
        [0]: {Name = "List`1" FullName = "System.Collections.Generic.List`1[[AssemblyNameXYZ.Item, AssemblyNameXYZ, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"}
        [1]: {Name = "Item" FullName = "AssemblyNameXYZ.Item"}
        [2]: {Name = "String" FullName = "System.String"}
        [3]: {Name = "Int32" FullName = "System.Int32"}
        [4]: {Name = "SubClass" FullName = "AssemblyNameXYZ.SubClass"}
    

    I didn’t make comprehensive tests but this should at least point you to the right direction. Fun question. I have fun answering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a big multi-module project, and I'd like to generate a report that
I've got one really big .java class file that has a lot of members.
I've got some big C programs, and I would like to know when I'm
Working on a database class that got quite big, and is likely to get
I've got a big big code base that includes two main namespaces: the engine
So I've got a big text file which looks like the following: <option value
I've got a big-ish project that needs a lot of work on a new
Here's the scenario: I've got a big file filled with all sorts of eclectic
I've got a big project written in PHP and Javascript. The problem is that
I've got a big Javascript project that I'm trying to refactor into pseudo-classes: jsFiddle:

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.