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 属性设置为适当的值。

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

属性

ExtensionData

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

适用于

另请参阅