How to ignore unknown types using DataContractSerializer

I am trying to use the DataContractSerializer in my application to be compatible with reverse and direct access and to support two-way travel (if possible).

Is it possible to support a two-way trip, or if not, is it possible to simply ignore unknown types in the following scenario?

Suppose I have a ClassWithObject class that has a property of a type object, and an older version of my application stores an object of type CurrentAdditionalData in this property.

[DataContract] [KnownType(typeof(CurrentAdditionalData))] public class ClassWithObject : IExtensibleDataObject { #region IExtensibleDataObject Members private ExtensionDataObject extensionDataObject_value; [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public ExtensionDataObject ExtensionData { get { return extensionDataObject_value; } set { extensionDataObject_value = value; } } #endregion [DataMember] public object AdditionalData { get; set; } } [DataContract] public class CurrentAdditionalData : IExtensibleDataObject { #region IExtensibleDataObject Members private ExtensionDataObject extensionDataObject_value; [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public ExtensionDataObject ExtensionData { get { return extensionDataObject_value; } set { extensionDataObject_value = value; } } #endregion [DataMember] public int MyProperty { get; set; } } 

For the new version of my application, there is no need to download this file, since it knows the CurrentAdditionalData class.

But what if the new version stores an object of type FutureAdditionalData that the old version does not know?

  [DataContract] public class FutureAdditionalData : IExtensibleDataObject { #region IExtensibleDataObject Members private ExtensionDataObject extensionDataObject_value; [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public ExtensionDataObject ExtensionData { get { return extensionDataObject_value; } set { extensionDataObject_value = value; } } #endregion [DataMember] public string Property1 { get; set; } [DataMember] public float Property2 { get; set; } [DataMember] public double Property3 { get; set; } } 

If the old version tries to read this file, it will get a SerializationException because it does not know this type.

Is it possible to modify the old version so that it knows unknown types and simply ignores them?

Or even better, is it possible to load an unknown object into ExtensionData and write it unchanged if the old version saves the file again?

+4
source share
1 answer

Microsoft has a complete guide to solving these problems with Data Contracts. Check out: https://msdn.microsoft.com/en-us/library/ms731138.aspx

I just realized that you meant that you are changing dataType, and not just adding / removing an element. No, you deal with it accordingly by creating a new member for a new type, rather than modifying an existing one. Modifying Types data is a violation of the changes to the API.

Check out this guide on what changes constitute a version change for the API: http://blogs.msdn.com/b/craigmcmurtry/archive/2006/07/23/676104.aspx

0
source

All Articles