I have an IEnumerable<IDisposable>
collection that I need to dispose of at a given point. The issue is that for some cases all the object will be of one type and disposing of the objects needs to be done by collecting some data from them and making a single RPC call. In other cases, the objects will be of another type and I just need to call Dispose on each of them.
I control the code doing this but I'd rather not push implementation details into it.
If I have to I can switch to something like this but it seems inelegant.
with(var rpc = new RPCDisposer())
{
foreach (var item in Items)
{
rpc.Add(item);
item.Dispose();
}
}
Edit: the list (for now) will only contain one type or the other, never both. But I'd rather the Dispose code not have to known about the RPC stuff at all, e.i.:
foreach (var item in Items)
item.Dispose();