Is there a way to serialize (using BinaryFormatter
) the IEnumerator<T>
that gets created when I use yield return
s? The autogenerated class isn't marked Serializable
.
views:
60answers:
3
+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
2010-07-01 22:13:00
+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
2010-07-01 22:29:01