views:

60

answers:

3

Is there a way to serialize (using BinaryFormatter) the IEnumerator<T> that gets created when I use yield returns? The autogenerated class isn't marked Serializable.

A: 

Just iterate the enumerator and serialize each instance returned.

codekaizen
+2  A: 

Which class are you using to implement IEnumerable<T>? List<T> should serialize OK.

Try calling ToList() on your collection before serializing it.

Robert Harvey
+3  A: 

The compiler generated enumerable does not have the [Serializable] attribute applied to it, so, no, out of the box you cannot do this.

You should ask yourself, "Why am I using a yield here, and what does it mean once this object is deserialized?"

If its convenience, you can use it to fill a type that can be serialized. If you use it to perform some heavy lifting in a lazy manner, you might want to consider changing your design to serialize/deserialize the information you need to perform that heavy lifting.

Will