I'm using the DataContractSerializer for storing an object of my own class to save user settings. The UserSettings class uses some other objects of different data types. I add all of those types to the KnownTypes parameter of the DataContractSerializer wich works just fine. But i've got issues with forward compatibility. If i introduce a new property in the UserSerrings class using a new data type it also works in the new version of my program. But the old version throws an exception saying that the XML file cannot be deserialized because the d开发者_运维技巧ata type is not in the KnownTypes array. I simply want to ignore those types because they're not used in the old version anyway. How can i achieve this, so that the old version can read and write the xml file ignoring those unknown properties/types?
This is exactly the scenario that IExtensibleDataObject (extension data) was designed for! The idea is that if the v1 of the data contract is decorated with the extension data interface, it will automagically ignore, store, and round-trip data from future revisions of the data contract -- including future unknown types -- without any issues.
I'd recommend the following resources to learn more about this feature and to learn how to implement it for your particular situation:
Forward-compatible data contracts
Data contract versioning
IExtensibleDataObject sample
And even more guidance on data contract versioning...
精彩评论