序列化的DataContract向后兼容性

本文关键字:兼容性 DataContract 序列化 | 更新日期: 2023-09-27 18:29:55

我有一个这样的类:

[DataContract(Namespace = "blah")]
public class Item
{
    [DataMember(Order = 0)]
    public int Index { get; set; }
    [DataMember(Order = 1)]
    public Uri ItemSource { get; set; }
    [DataMember(Order = 2)]
    public Uri ErrorSource { get; set; }
}

我有很多它的序列化副本(在文件中)(包括一些生产文件),现在我的任务是将这个类更改为以下类:

[DataContract(Namespace = "blah")]
public class Item
{
    [DataMember(Order = 0)]
    public int Index { get; set; }
    [DataMember(Order = 1)]
    public ItemSourcesCollection Sources { get; set; }
}

其中ItemsCollection是

[CollectionDataContract(ItemName = "ItemSourceItem", Namespace = "blah")]
public class ItemSourcesCollection : List<ItemSource> {}

其中ItemSource是

[DataContract]
public class ItemSource
{
    [DataMember]
    public Uri SourcePath { get; set; }
    [DataMember]
    public ItemSourceType Type { get; set; }
}

其中ItemSourceType是

[Serializable]
public enum ItemSourceType 
{
    Data,
    Errors
}

问题是向后兼容性。是否有可能对旧的序列化项进行了正确的反序列化?迁移具有向后兼容性的数据契约的最佳实践/模式是什么?

序列化的DataContract向后兼容性

是的,有一些反射是可能的。我正在做以下事情,以便在反序列化期间手动检查以前的版本。

首先对项目类使用IExtensibleDataObject:

[DataContract(Namespace = "blah")]
public class Item : IExtensibleDataObject
{
    [DataMember(Order = 0)]
    public int Index { get; set; }
    [DataMember(Order = 1)]
    public ItemSourcesCollection Sources { get; set; }
}

现在反序列化方法的棘手问题是:

    /// <summary>
    /// The deserialized.
    /// </summary>
    /// <param name="context">
    /// The streaming context.
    /// </param>
    [OnDeserialized]      
    private void Deserialized(StreamingContext context)
    {
       // reflection for backward compatibilty only
        if (this.ExtensionData == null)
        {
            return;
        }
        IList members = this.CheckForExtensionDataMembers();
        if (members == null)
        {
            return;
        }
        string value = this.GetExtensionDataMemberValue(members, "ItemSource");
        // do something with value
        value = this.GetExtensionDataMemberValue(members, "ErrorSource");
        // do something with value
    }
    /// <summary>
    /// The check for extension data members.
    /// </summary>
    /// <returns>
    /// Thel list of extension data memebers.
    /// </returns>
    private IList CheckForExtensionDataMembers()
    {
        PropertyInfo membersProperty = typeof(ExtensionDataObject).GetProperty(
            "Members", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
        var members = (IList)membersProperty.GetValue(this.ExtensionData, null);
        if (members == null || members.Count <= 0)
        {
            return null;
        }
        return members;
    }
    /// <summary>
    /// The get extension data member value.
    /// </summary>
    /// <param name="members">
    /// The members.
    /// </param>
    /// <param name="dataMemberName">
    /// The data member name.
    /// </param>
    /// <returns>
    /// Returns extension data member value.
    /// </returns>
    private string GetExtensionDataMemberValue(IList members, string dataMemberName)
    {
        string innerValue = null;
        object member =
            members.Cast<object>().FirstOrDefault(
                m =>
                ((string)m.GetType().GetProperty("Name", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(m, null)).Equals(
                    dataMemberName, StringComparison.InvariantCultureIgnoreCase));
        if (member != null)
        {
            PropertyInfo valueProperty = member.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
            object value = valueProperty.GetValue(member, null);
            PropertyInfo innerValueProperty = value.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Public);
            object tmp = innerValueProperty.GetValue(value, null);
            var s = tmp as string;
            if (s != null)
            {
                innerValue = s;
            }
        }
        return innerValue;
    }

以上所有内容都将在项目类中。