0

I am trying to iterate through a list of objects and only change the ones that match a particular type. My current code looks like this. (Platform is an extension of Entity, and entities is a list of type Entity)

foreach (Platform p in entities.OfType<Platform>) { p.doStuff() }

I am getting the error "foreach cannot opperate on a 'method group'" Thanks for anyone's help. :)

4

2 回答 2

3

Alright then :

foreach (Platform p in entities.OfType<Platform>())
 //Will loop through all object of Platform type in entites.OfType<Platform>()
于 2013-04-19T20:36:09.413 回答
1

You can use LINQ and the "is" and "as" keywords.

foreach (object o in entities.Where(x => x is Platform))
{
    Platform p = o as Platform;
    p.doStuff();
}
于 2013-04-19T20:39:00.137 回答