Deserialization of optional fields from BinaryFormatter

后端 未结 1 1762
青春惊慌失措
青春惊慌失措 2021-01-13 19:11

I have an application that serializes data using BinaryFormatter. A member was added to the class that was serialized from one version to the next without chan

相关标签:
1条回答
  • 2021-01-13 19:50

    If you mark the new fields with [OptionalField] it should work, but I have heard reports of flakiness in some cases. I can't say for sure, since I avoid BinaryFormatter, because it has so many issues when versioning :) (plus, it isn't as "tight" as some alternatives, and has severe issues if you want to go cross-platform, or to CF/SL etc)

    If you are implementing ISerializable, you might try:

    foreach(SerializationEntry entry in info) {
        switch(entry.Name) {
             case "Name": Name = (string)info.Value;
             case "Id": Id = (int)info.Value;
             ...
        }
    }
    

    But again, must stress - this is doing things the hard way :p With this approach, you only process the data that is actually there.

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