3

According to a mspress book (MCTS for Exam 70-536 .NET 2.0):

You might have version compatibility issues if you ever attempt to deserialize an object that has been serialized by an earlier version of your application. Specifically, if you add a member to a custom class and attempt to deserialize an object that lacks that member, the runtime will throw an exception. In other words, if you add a member to a class in version 3.1 of your application, it will not be able to deserialize an object created by version 3.0 of your application.

Now... As curious as I am I went and created a project, serialized a class, added a new member and attempted to deserialize the class to the new object. To my surprise it worked and the newly created member was set to null by default (even if it had another default value).

I first tried adding a String as the new member, seeing that worked I then changed it to another custom class and it still worked.

Is the mspress wrong? Or is this behavior customizable?

Please note that I am NOT using XML serialization. I'm using BinaryFormatter Serialization.

5
  • Which kind of serialization did you use? AFAIK some forms like binary have this problem, while other forms like XML don't.
    – user541686
    Jan 23, 2011 at 20:37
  • I am using BinaryFormatter Serialization.
    – PedroC88
    Jan 23, 2011 at 20:57
  • 1
    To clarify - I've just repro'd; serialized from v1, added a field in v2 without [OptionalField] and deserialized - no exception. Essentially, then, [OptionalField] seems pointless ;p Jan 23, 2011 at 21:23
  • That's why. AFAIK binary formatting requires an exact match, simply because of how it works. You'd need to switch to something non-version-breaking like XML if you need that functionality.
    – user541686
    Jan 24, 2011 at 3:20
  • @Mehrdad I think you are getting it backwards... deserialization works even if there's no 1:1 match... on BinaryFormatter. I'll try with different serialization techniques to see if the others brake...
    – PedroC88
    Jan 24, 2011 at 22:21

3 Answers 3

2

Mspress says the opposite. Serialize the class, delete a member, deserialize, and that won't work. Basically it will read the members from the stream, and throw an exception when it can't find one. The other way around it doesn't care if there are fields which weren't present in the stream.

1
  • Can you give me the link towards that info? The same MSPress book later reads: "The .NET Framework 2.0 is capable of deserializing objects that have unused members, so you can still have the ability to deserialize an object if it has a member that has been removed since serialization. [...]".
    – PedroC88
    Jan 23, 2011 at 20:48
2

BinaryFormatter is (frankly) very fussy, but version tolerant serialization was added - the idea was simply to add [OptionalField] to those fields that you expected to be null.

However, if you are saying it deserialized as null, maybe even this was proving too painful, and they dimply made "optional" the default behaviour.

Note that the many foibles of BinaryFormatter (plus the fact that it is not platform-independent) are why I just don't use it ;p

1

I kept on running test with serialization and came to agree with @Mehrdad on that some Serialization processes support deserialzing objects that now have new members (although he said it backwards).

BinaryFormatter serialization, just like Marc Gravell explained, seems to indeed support Version Tolerant deserialization, I also tested it to work with XMLSerialization. Please note that XMLSerialization only serializes and deserializes public members.

SoapFormatter, on the other hand, does not support this type of behavior and it does throw an exception when deserializing an object with newly added members.

I haven't ran no test with Custom Serialization but due to it's nature, purpose and design it should be, as well, version torelant on it's own.

I think that just for the sake of being in the safe side you should always use the [OptionalField] attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.