The book CLR Via C#
presents a simple way to clone objects via binary serialization.
It specifies StreamingContextStates.Clone
when creating the BinaryFormatter
like so:
var formatter = new BinaryFormatter
{
Context = new StreamingContext(StreamingContextStates.Clone)
};
The documentation for StreamingContextStates.Clone
says that it
Specifies that the object graph is being cloned. Users can assume that the cloned graph will continue to exist within the same process and be safe to access handles or other references to unmanaged resources.
Well fair enough - but I don't really know what this actually means. In what way does this actually change the behaviour of the BinaryFormatter
? Can anyone list any concrete effects that using this flag has?
BinaryFormatter
in, well, most circumstances. It has some things where it is handy, but I've seen so many people have so much pain caused from this... indeed, the issues inBinaryFormatter
were part of why I got heavily involved in serialization a few years back – SnatchISerializable.GetObjectData(...)
, so technically any implementation could in theory choose to do something different based on theStreamingContextStates
value. I don't know how many, if any, do, though – Snatch