如何使用 DataContractSerializer 忽略未知类型 [英] Howto ignore unknown types with DataContractSerializer

查看:50
本文介绍了如何使用 DataContractSerializer 忽略未知类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的应用程序中使用 DataContractSerializer,以便向后和向前兼容并支持往返(如果可能).

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?

假设我有一个 ClassWithObject 类,它有一个 object 类型的属性,并且我的应用程序的旧版本在这个属性中存储了一个 CurrentAdditionalData 类型的对象.

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; }
}

对于我的应用程序的新版本,加载这个文件没有问题,因为它知道类 CurrentAdditionalData.

For the new version of my application it is no problem to load this file, since it knows the class CurrentAdditionalData.

但是如果新版本存储了一个旧版本不知道的 FutureAdditionalData 类型的对象怎么办?

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; }
}

如果旧版本试图读取这个文件,它会得到一个 SerializationException,因为它不知道这个类型.

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?

或者更好的是,如果旧版本再次保存文件,是否可以将未知对象加载到ExtensionData中并原封不动地写出来?

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?

推荐答案

Microsoft 有一个完整的指南来解决这些类型的数据契约问题.查看:https://msdn.microsoft.com/en-us/library/ms731138.aspx

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

我刚刚意识到您的意思是更改数据类型,而不是简单地添加/删除成员.不,您可以通过为新类型创建新成员而不是更改现有成员来适当地处理此问题.更改数据类型是 API 的重大更改.

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.

查看此指南,了解哪些更改构成对 API 的版本化更改:http://blogs.msdn.com/b/craigmcmurtry/archive/2006/07/23/676104.aspx

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

这篇关于如何使用 DataContractSerializer 忽略未知类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆