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.
Well if it's actually enumerable, presumably it implements IEnumerable
... so you can just use:
foreach (object item in (IEnumerable) MyList)
{
...
}
You're not going to have a better compile-time type for item
than object
anyway...
See more on this question at Stackoverflow