Removing object deserialization with new members

According to mspress (MCTS for exam 70-536.NET 2.0):

You may have version compatibility issues if you try to deserialize an object that was serialized by an earlier version of your application. In particular, if you add an element to a custom class and try to deserialize an object that does not have this member, the runtime will throw an exception. In other words, if you add a member to the version 3.1 class of your application, he will not be able to deserialize the object created in version 3.0 of your application.

Now ... Oddly enough, I went and created a project, serialized the class, added a new member and tried to deserialize the class for a new object. To my surprise, this worked, and the newly created member was set to the default by default (even if it had a different default value).

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

Is mspress wrong? Or is this behavior customizable?

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

+3
source share
3 answers

@Mehrdad , Serialization , ( ).

BinaryFormatter , , -, Tolerant, XMLSerialization. , XMLSerialization .

SoapFormatter, , , .

, - , torelant.

, , , [OptionalField].

+1

Mspress . , , , . , , . , , .

+2

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

However, if you say that it is deserialized as null, maybe even that turned out to be too painful, and they dimly made the default behavior “optional”.

Please note that many shortcomings BinaryFormatter(plus the fact that it does not depend on the platform) is why I simply do not use it: p

+2
source

Source: https://habr.com/ru/post/1787332/


All Articles