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?
source share