语言

IExtensibleDataObject 接口

定义

提供一个数据结构,用于存储用XmlObjectSerializer属性标记的类型反序列化期间遇到的DataContractAttribute额外数据。

public interface class IExtensibleDataObject
public interface IExtensibleDataObject
type IExtensibleDataObject = interface
Public Interface IExtensibleDataObject
派生

示例

以下代码显示了类型(PersonVersion2)的实例,该实例是可序列化类型的第二个版本(Person)。 第二个版本包含第一个版本中不存在的额外数据(ID 字段)。

// Implement the IExtensibleDataObject interface
// to store the extra data for future versions.
[DataContract(
    Name = "Person",
    Namespace = "http://www.cohowinery.com/employees")]
class Person : IExtensibleDataObject
{
    // To implement the IExtensibleDataObject interface,
    // you must implement the ExtensionData property. The property
    // holds data from future versions of the class for backward
    // compatibility.
    private ExtensionDataObject extensionDataObject_value;
    public ExtensionDataObject ExtensionData
    {
        get
        {
            return extensionDataObject_value;
        }
        set
        {
            extensionDataObject_value = value;
        }
    }
    [DataMember]
    public string Name;
}

// The second version of the class adds a new field. The field's
// data is stored in the ExtensionDataObject field of
// the first version (Person). You must also set the Name property
// of the DataContractAttribute to match the first version.
// If necessary, also set the Namespace property so that the
// name of the contracts is the same.
[DataContract(Name = "Person",
    Namespace = "http://www.cohowinery.com/employees")]
class PersonVersion2 : IExtensibleDataObject
{
    // Best practice: add an Order number to new members.
    [DataMember(Order=2)]
    public int ID;

    [DataMember]
    public string Name;

    private ExtensionDataObject extensionDataObject_value;
    public ExtensionDataObject ExtensionData
    {
        get
        {
            return extensionDataObject_value;
        }
        set
        {
            extensionDataObject_value = value;
        }
    }
}
' Implement the IExtensibleDataObject interface 
' to store the extra data for future versions.
<DataContract(Name := "Person", [Namespace] := "http://www.cohowinery.com/employees")>  _
Class Person
    Implements IExtensibleDataObject
    ' To implement the IExtensibleDataObject interface,
    ' you must implement the ExtensionData property. The property
    ' holds data from future versions of the class for backward
    ' compatibility.
    Private extensionDataObject_value As ExtensionDataObject
    
    Public Property ExtensionData() As ExtensionDataObject _
       Implements IExtensibleDataObject.ExtensionData
        Get
            Return extensionDataObject_value
        End Get
        Set
            extensionDataObject_value = value
        End Set
    End Property
    <DataMember()>  _
    Public Name As String
End Class 

' The second version of the class adds a new field. The field's 
' data is stored in the ExtensionDataObject field of
' the first version (Person). You must also set the Name property 
' of the DataContractAttribute to match the first version. 
' If necessary, also set the Namespace property so that the 
' name of the contracts is the same.

<DataContract(Name := "Person", [Namespace] := "http://www.cohowinery.com/employees")>  _
Class PersonVersion2
    Implements IExtensibleDataObject

    ' Best practice: add an Order number to new members.
    <DataMember(Order:=2)>  _
    Public ID As Integer
    
    <DataMember()>  _
    Public Name As String
    
    Private extensionDataObject_value As ExtensionDataObject
    
    Public Property ExtensionData() As ExtensionDataObject _
       Implements IExtensibleDataObject.ExtensionData
        Get
            Return extensionDataObject_value
        End Get
        Set
            extensionDataObject_value = value
        End Set
    End Property
End Class

注解

该 IExtensibleDataObject 接口提供一个属性,用于设置或返回用于存储数据协定外部的数据的结构。 额外的数据存储在类的 ExtensionDataObject 实例中,并通过 ExtensionData 属性访问。 在接收、处理和返还数据的往返操作中,额外数据会完整地返还给原始发送方。 这对于存储从合同的未来版本接收的数据非常有用。 如果未实现接口,在往返操作过程中,任何额外的数据将会被忽略和丢弃。

使用此版本管理功能

  1. 在 IExtensibleDataObject 类中实现接口。

  2. 将 ExtensionData 属性添加到您的类型中。

  3. 在类中添加一个类型为 ExtensionDataObject 的私有成员。

  4. 使用新的私有成员为该属性实现 get 和 set 方法。

  5. 将 DataContractAttribute 属性应用于类。 如有必要,请将和NameNamespace属性设置为适当的值。

有关类型版本控制的详细信息,请参阅 数据协定版本控制。 有关创建与转发兼容的数据协定的信息,请参阅 Forward-Compatible 数据协定。 有关数据协定的详细信息,请参阅 “使用数据协定”。

属性

名称 说明
ExtensionData

获取或设置包含额外数据的结构。

适用于

另请参阅