Iterating through an object which contains a list

Posted on 2009-04-23
Last Modified: 2012-06-21
I have an object of type object which I know contains a list (both because of the nature of the problem and because I can see it in the debugger).  I want to be able to iterate through the members of the list.

My problen is that I can't work how to cast the object onto an interface that would allow iteration. I have tried:

List<object> objList = (List<object>)objectContainingList;
System.Array variantArray = (Array)objectContainingList;

//Simplified example of the problem:

//Generate a list containing integers

List<int> intList = new 




//Loose all info about the object, remember this is a hypothetical example!

object objectContainingList = intList;

//Now I want to loop through the object

foreach (object obj in objectContainingList)




Open in new window

Question by:erik_bradshaw
    LVL 39

    Expert Comment

    You need to do the following for this to work (you changed the List object to a type of object, not the items in that list):

    List<int> castedList = (List<int>) objectContainingList;
    foreach (int myInt in castedList )
       Console.WriteLine(myInt .ToString();

    Open in new window


    Author Comment

    The problem is a bit more complex than my simple example. I don't know either the type of object contained in the parent object nor the type of enumerable object, hence objectContainingList could be things like:

    Any member of System.Collections

    All I know for certain is that the parent object (List<int> in the example above) supports the IEnumerable interface
    LVL 39

    Accepted Solution

    Then you can do the following:

    IList<int> someList = new List<int>();
    // loose type info of both content and list
    object oUnknownEnumerableType = (object)someList;
    // but we know it is of type IEnumerable
    IEnumerable castedEnum= (IEnumerable) oUnknownEnumerableType;
    foreach (object oo in castedEnum)

    Open in new window

    LVL 39

    Expert Comment

    The above will print 10 and 12 to the debug output window. Note that the following would NOT work:
    IEnumerable<object> castedEnum= (IEnumerable<object>) oUnknownEnumerableType;
    because there's no explicit conversion to cast everything inside the list to another type, even though you request the "cath-all" type of object.

    Author Closing Comment

    Worked great, thanks for all your help. It's so easy, can't believe I spent so much time battling with reflection to try and sort it out!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now