Home:ALL Converter>How to enumerate Object?

How to enumerate Object?

Ask Time:2014-08-21T00:46:14         Author:Pablo

Json Formatter

I am trying to enumerate Object which is actually enumerable, but the type is stored in Type variable

public Type MyListType { get; set; }
public Object MyList { get; set; }
...
foreach (var item in Convert.ChangeType(MyList, MyListType))
{
...
}

This obviously gives an error because ChangeType still returns Object. How can I cast MyList into enumerable type, particularly of MyListType?

Update:

To be more clear, Object is BindingList<T> type, where T is residing in MyListType.

Author:Pablo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25410203/how-to-enumerate-object
Jon Skeet :

Well if it's actually enumerable, presumably it implements IEnumerable... so you can just use:\n\nforeach (object item in (IEnumerable) MyList)\n{\n ...\n}\n\n\nYou're not going to have a better compile-time type for item than object anyway...",
2014-08-20T16:48:30
yy