Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Best approach for version tolerant serialization when new field allows null but has another default value.

$
0
0

I had asked a similar question:

Thread Title Determining OptionalFieldAttribute.VersionAdded for field being deserialized 

But it was inadvertently posted to the wrong forum (not a WCF question) and other than one reply I seemed to be having a conversation with myself.

I would like to implement a robust serialization mechanism without having to do too much work and the BinaryFormatter seems to work well - except when the default value of a new variable is not null or zero.

Take for example the case of adding a bool variable to an existing class class:

[OptionalField(VersionAdded = 2)]
private bool doGood = true;

When deserializing, if the version serialized was was of an earlier rev it will be de-serialized with a value of 'false' and to correct this in a method attributed with the Deserialized attribute it would be necessary to know this - hence the title of my original question.

I have given up on discovering an easy way to determine the version of the saved class and am now asking - what is the recommended way to serialize a field such as this - with the least amount of hassle & maintenance.

This must be a situation encountered by many developers so I assume that there are some recommendations or guidelines for how to do this.

I proposed saving a dictionary with the full type name mapped to a version number but if was possible to force the BinaryFormatter to overwrite a static class variable that had already been initialized then that would appear to me as simpler and easier to maintain.


Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>