Is it generally a bad idea to pass an IEnumerable across appdomain boundaries?
I ask because with my current understanding of IEnumerable implementations, the enumerator isn’t going to be used until the collection is, well, enumerated. When you are crossing appdomain boundaries, particularly involving multiple processes, would this not result in multiple trips across the boundary, one for every item returned? If that is the case, then returning the collection in its entirety, when possible, such as in an array, would be preferable in terms of performance, would it not?
First, it depends how the object to be enumerated is: whether is inherits from
MarshalByRefor if it is serializable. In the second case, a copy is passed to the other appdomain, which then resembles the array approach. On the other hand, if it inherits fromMarshalByRef, it pretty much depends how the enumerator is accessing the owner instance.So in general, I’d say that you should only pass
IEnumerables accross appdomains if you do know what to expect. Otherwise, you may get unexpected results or bad performance.