Howto ignore unknown types with DataContractSerializer

微笑、不失礼 提交于 2021-02-19 03:12:44

问题


I try to use DataContractSerializer in my application in order to be backward and forward compatible and to support round trip (if possible).

Is it possible to support round trip, or if not, is it possible to just ignore unknown types in the following scenario?

Suppose I have a class ClassWithObject that has a property of type object and the older version of my application stored 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 it is no problem to load this file, since it knows the class CurrentAdditionalData.

But what if the new version stores an object of type FutureAdditionalData, that the old version doesn't 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 doesn't know this type.

Is it possible to modify the old version in such a way, that it is aware of unknown types and simply ignores them?

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


回答1:


Microsoft has a full guide for solving these kinds of issues with Data Contracts. Check out: https://msdn.microsoft.com/en-us/library/ms731138.aspx

I just realized you meant you change the dataType, and not simply adding/removing the member. No, you handle this appropriately by making a new member for the new type, not by changing the existing one. Changing dataTypes is a breaking change for an API.

Check out this guidance about what changes constitute a versioned change to an API: http://blogs.msdn.com/b/craigmcmurtry/archive/2006/07/23/676104.aspx



来源:https://stackoverflow.com/questions/14892113/howto-ignore-unknown-types-with-datacontractserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!