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
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:
Type.IsPrimitive).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())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:
I didn’t make comprehensive tests but this should at least point you to the right direction. Fun question. I have fun answering.