?
Solved

Iterating through an object which contains a list

Posted on 2009-04-23
5
Medium Priority
?
416 Views
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 
intList.Add(1);
intList.Add(5);
intList.Add(7);
 
//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)
{
   Console.WriteLine(obj.ToString();
}

Open in new window

0
Comment
Question by:erik_bradshaw
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24213273
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

0
 

Author Comment

by:erik_bradshaw
ID: 24213424
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:

System.Array
System.Data.Linq.Table
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
0
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24213504
Then you can do the following:



IList<int> someList = new List<int>();
someList.Add(10);
someList.Add(12);
 
// 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)
{
    Debug.WriteLine(oo.ToString());
}

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24213517
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.
0
 

Author Closing Comment

by:erik_bradshaw
ID: 31573695
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!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

864 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