views:

49

answers:

2

I need to be able to loop around an unknown type for example

  foreach (var test in viewData["foobar"])
  {
  }

Any suggestions

+2  A: 

You have to at least cast viewData["foobar"] to IEnumerable to have objects in your test variable.

The cast may fail, so you'll first have to check whether viewData["foobar"] actually implements IEnumerable with is or as operator:

if(viewData["foobar"] is IEnumerable)
    foreach(var test in (IEnumerable)viewData["foobar"])
Anton Gogolev
Assuming this is C# we're talking about, of course.
BlueRaja - Danny Pflughoeft
+1 simple straightforward
JonH
viewData["foobar"] might already be an IEnumerable if viewData's named indexer returns a type (derived from) IEnumerable.
Webleeuw
A: 

If viewData["foobar"] is of the type object, then you can't iterate over it. The only way to iterate with a foreach loop is on IEnumerator derived types.

Webleeuw