Versioning Tolerant (De)serialization
-
In .NET 2.0 great strives have been made to make binary serialization more version tolerant, with new or removed fields no longer throwing exceptions. Although one scenario that keeps throwing an exception... when the type of a field has changed. Using OptionalFieldAttribute one can indicate to omit during deserialization a new field, if the data for that field is not present in the old stream. Is there any declarative (such as TypeConverterAttribute) or imperative mechanism to handle situations when the type of the field has changed, to prevent exception from throwing when data type of one or more fields has changed? Essentially to ignore reading data for fields that have different type between definition in old serialized stream and new definition in assembly. Thanks in advance
Deviation from good code design leads to the dark side (aka kludgy code). Mike M MCAD.NET WinInsider.com - News for Microsoftonians
-
In .NET 2.0 great strives have been made to make binary serialization more version tolerant, with new or removed fields no longer throwing exceptions. Although one scenario that keeps throwing an exception... when the type of a field has changed. Using OptionalFieldAttribute one can indicate to omit during deserialization a new field, if the data for that field is not present in the old stream. Is there any declarative (such as TypeConverterAttribute) or imperative mechanism to handle situations when the type of the field has changed, to prevent exception from throwing when data type of one or more fields has changed? Essentially to ignore reading data for fields that have different type between definition in old serialized stream and new definition in assembly. Thanks in advance
Deviation from good code design leads to the dark side (aka kludgy code). Mike M MCAD.NET WinInsider.com - News for Microsoftonians
Not sure if such attribute exists. If one does not exist, you can write your own. Or you can catch the exception during the Deserializing event which is raised before deserialization. You can use the attribute: [OnSerializing] void TypeHasChanged() { //Handle type change exception }