What's up with the [OptionalField] Attribute?

后端 未结 1 933
萌比男神i
萌比男神i 2020-12-30 10:33

As I understand it I have to adorn a new member in a newer version of my class with the [OptionalField] Attribute when I deserialize an older version of my class that lacks

相关标签:
1条回答
  • 2020-12-30 11:20

    BinaryFormatter is, at the best of times, very brittle if you change things. Not least, there are huge problems with automatically implemented properties, obfuscaction, renaming, strong naming, etc.

    As I recall, some of the rules about [OptionalField] changed just before it was released; the version tolerant thing didn't really work out as easily as had been planned, I expect.

    My advice: if you want version tolerant serialization (i.e. you can serialize it today and deserialize it with the next version of your app), then don't use BinaryFormatter; this is (IMO) only suitable for passing data between the same version (remoting, AppDomains, etc).

    For work between versions, I recommend contract-based serialization; things like XmlSerializer and DataContractSerializer (.NET 3.0), or for binary - protobuf-net or similar tools. All of these are much better at version tolerance (indeed, you don't even need to deserialize it into the same Type); plus they can be used between platforms - so you can serialize in .NET and deserialize in java/C++/etc.

    0 讨论(0)
提交回复
热议问题