How to recognize versions of objects placed in isolated storage using .NET runtime serialization?
We are building application that stores objects to isolated storage using .NET runtime serialization.
Problems occur when we update application by adding some new properties to the classes of objects we are serializing. So we want to do some kind of versioning of the objects in isolated storage so we can check if they are obsolete before they are deserialized.Any advice and ideas how to do this on best possi开发者_JAVA技巧ble way?
What do you think about custom formatter implementing IFormatter interface and can it help instead of vesioning objects?
I wrote about this issue on MS forum more detailed here.
You COULD have a serialization in the serialization. First a wrapper class telling the version, and holding the inner true class.
This however feels a bit bad smelly..
Here are a few options (at in any particular order).
- Name the file based on the version
- Place the file in a directory based on a version
- Create a wrapper object that contains metadata about each serialized object such as the version number.
- Add a property to each object that contains the persisting application's version number
If its binary serialization, you could read the bytes directly, and determine the assembly version from this. Byte number 22 onwards contains information on the assembly and object type, so you could write something that would read this, and then determine if your objects are obsolete.
Marc Gravell was propose in comment great idea to use version-tolerant serializer. It enables enough control of deserialization for us even to make obsolete objects reusable. More on msdn
Thanks to all for suggestions.
精彩评论