I've got a List of objects which have a method that's being ran. Some objects' method remove other objects from the list, which causes an InvalidOperationException.
The code somewhat (simplified) looks like this:
static List<myObject> olist = new List<myObject>();
// fill list
foreach (myObject obj in olist) {
obj.func();
}
A func() of an obj may contain something like this:
olist.Remove(specificListItem);
Which obviously causes the exception above. Right now, the current setup of having a foreach run the objects' own methods have to stay - what workaround would be the best here?
Since we're running a method of undetermined (at the time of beginning the foreach) function - inside of an object, we can't use tricks live reversing the order mentioned here.