I need to serialize some object graphs to disc
What difficulties am I likely to encounter if I make changes to a class, then try to deserialize an old version?
Do some serializers handle this better than others?
What is the standard way of handling such a scenario?
For example, in a new version of the code, do I need to retain all开发者_运维问答 the old classes so that when deserializing an old file i can do so to the old class, then migrate the data to the new class? Will changing the class' name/namespace break deserialization?
Thanks for any advice
Well, as I remember, the problems starts after:
- Adding a new field/property to a class;
- Changing the type of an existing field/property.
You can have custom serialization to handle versioning problems on you own.
But I would recommend to use DataContractSerializer
along with Best Practices: Data Contract Versioning for the most common cases.
This kind of depends upon what format you need the serialised data in. If you use .NET 1.0 binary serialisation, then you'll be limited to a specific version of the DLL. I wouldn't recommend that.
Personally I'd suggest using DataContracts with the default WCF serialiser: DataContractSerialiser
. You can control what happens when you deserialise a different version of the type with techniques like [OnDeserializing]
and IExtensibleDataObject
.
You can have DataContractSerializer
output XML or binary too.
精彩评论