Serializable classes and name refactoring

In my current project, I use FxCop to work with various code analysis warnings. For naming rules, I have the opportunity to either change the names in accordance with the rules, or decide to keep my current spelling and suppress the warning. As for classes or attributes marked as [Serializable], I'm interested in name changes. In particular, how to affect backward compatibility with existing serialized data?

+3
source share
1 answer

If you change the field names and , to read the old data , serialized before your changes, then this can become messy. BinaryFormatter- This is basically a field serialized (personalized) serializer and it will not be happy. Changing properties and methods should be fine if it is not referenced by external code (and be sure to run your unit tests). Viable options at this point: manual serialization ( ISerializable) and serialization surrogates. A lot of pain.

If you use XmlSerializer/ DataContractSerialializer, you can name the serialization name separately for the member name, so it’s very easy to fix. And some other serializers don't use names at all; -p

XmlSerializer example:

[XmlElement("Color")] // original spelling
public string Colour {get;set;} // now with the correct spelling ;-p
+2
source

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


All Articles