The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Solving Binary serialization versioning conflicts

0 replies.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
Solving Binary serialization versioning conflicts Posted: Mar 3, 2005 6:23 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: Solving Binary serialization versioning conflicts
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

My friend, Jonathan Einav(His blog is, sadly, empty for now) just sent me this by email. It's a solution to a versioning problem he encountered, which he has made public for the community.
This is a a nice approach to deal with binary de-serialization type version changes (and avoid the iterating "Possible version mismatch. Type WindowsApplication1.MyClass has 3 members, number of members deserialized is 4"  SerializationException ).
Here's how he explains it::

Attached are the DeserializationDefaultValueAttribute.cs, DeserializationOldNameAttribute.cs, and the VersionSupportingBinaryFormatter.cs 

I created a reflection surrogate that de-serialize objects using reflection on the current compiled type definition from the loaded SerializtionInfo. I “tricked”  the BinarySerializer by writing a binder that aside from returning the exact type he received he creates the reflection surrogate for every type that the Serializer is deserializing (wouldn’t it be nice if a simple add surrogate with typeof(object) would work…)

Another trick I used for accessing private base class members values from the SerializationInfo.GetValue() is adding the base class name with a + on the recursive calls to the base classes (that’s how I saw the SerializationInfo  holds them, and for some reason it works….what ever happened to encapsulation…. (-: )    

 I also defined the:

  • DeserializationDefaultValueAttribute which can be placed above new fields that are added to new type definitions in order for the de-serialized new type instances from old type definitions will contain a default value (different from the one given by the CLR)
  • DeserializationOldNameAttribute which can be placed above a field that his name (not type!!!) was changed and we want the new field name to contain the old field value after deserialization.

Simply use the VersionSupportingBinaryFormatter.Deserialize() from an IFormatter point of view within a catch block of a BinarySerializer Deserialize attempt that failed due to a type definition change. (Or you can use it all the time, though it is slower then the Binary Formatter because it uses reflection)

This class was not fully tested yet, though it seems to be working fine in all scenarios that I have tested it (base classes, mutual references in the class graph, base private and protected value types and reference types etc).

Read: Solving Binary serialization versioning conflicts


Topic: More Great Non-Admin Resources Previous Topic   Next Topic Topic: Geek Notes 2005-03-01

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use