DataContract序列化的向后兼容性

时间:2012-04-03 11:38:57

标签: c# .net serialization backwards-compatibility datacontract

我有一个这样的课程:

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

其中ItemSourcesCollection是

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

问题是向后兼容性。是否有可能正确反序列化旧的序列化项目?通过向后兼容性迁移数据合同的最佳实践/模式是什么?

1 个答案:

答案 0 :(得分:1)

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

首先使用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;
    }

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