I've looked at similar questions but nothing quite fits. I have an object which happens to contain a List. I'd like to get it into something I can enumerate.
For example:
object listObject; // contains a List<Something>
List<object> list;
list = listObject as List<object>; // list contains null after
foreach ( object o in list )
{
// do stuff
}
The conversion from object to List<object> is the problem.
EDIT:
What I finished with:
object listObject; // contains a List<Something>
List<object> list;
IEnumerable enumerable = listObject as IEnumerable;
if ( enumerable != null )
{
list = enumerable.Cast<object>().ToList();
foreach ( object o in list )
{
// do stuff
}
}
尝试这个:
list = (listObject as IEnumerable).Cast<object>().ToList()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.