Search code examples
c#genericsreflectionnestedilist

C# - Reflection using Generics: Problem with Nested collections of ILists


I would like to be able to print object properties, and I've hit a snag when I hit nested collections of the iLists.

foreach (PropertyInformation p in properties)
            {
                //Ensure IList type, then perform recursive call
                if (p.PropertyType.IsGenericType)
                {
                         //  recursive call to  PrintListProperties<p.type?>((IList)p,"       ");
                }

Can anyone please offer some help?

Cheers

KA


Solution

  • I'm just thinking aloud here. Maybe you can have a non generic PrintListProperties method that looks something like this:

    private void PrintListProperties(IList list, Type type)
    {
       //reflect over type and use that when enumerating the list
    }
    

    Then, when you come across a nested list, do something like this:

    if (p.PropertyType.IsGenericType)
    {
       PringListProperties((Ilist)p,p.PropertyType.GetGenericArguments()[0]);
    }
    

    Again, haven't tested this, but give it a whirl...