IExtensibleDataObject 接口

定义

提供一个数据结构,用于存储 XmlObjectSerializer 在反序列化一个用 DataContractAttribute 属性标记的类型期间遇到的额外数据。Provides a data structure to store extra data encountered by the XmlObjectSerializer during deserialization of a type marked with the DataContractAttribute attribute.

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

示例

下面的代码演示了一个类型 (PersonVersion2) 的实例,该类型是可序列化类型 (Person) 的第二个版本。The following code shows an instance of a type (PersonVersion2) that is the second version of a serializable type (Person). 第二个版本包含第一个版本中没有的额外数据(ID 字段)。The second version contains extra data (ID field) that is not present in the first version.

// 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 接口提供了单个属性,该属性设置或返回一个用于存储数据协定外部数据的结构。The IExtensibleDataObject interface provides a single property that sets or returns a structure used to store data that is external to a data contract. 额外数据存储在 ExtensionDataObject 类的实例中,并且通过 ExtensionData 属性访问。The extra data is stored in an instance of the ExtensionDataObject class and accessed through the ExtensionData property. 在接收、处理和返回数据的往返操作中,额外数据被原封不动地返回到原始发送方。In a roundtrip operation where data is received, processed, and sent back, the extra data is sent back to the original sender intact. 这可用于存储从协定的将来版本接收的数据。This is useful to store data received from future versions of the contract. 如果您没有实现该接口,则会在往返操作中忽略和丢弃任何额外数据。If you do not implement the interface, any extra data is ignored and discarded during a roundtrip operation.

使用此版本管理功能To use this versioning feature

  1. 在类中实现 IExtensibleDataObject 接口。Implement the IExtensibleDataObject interface in a class.

  2. ExtensionData 属性添加到您的类型中。Add the ExtensionData property to your type.

  3. 将类型 ExtensionDataObject 的一个私有成员添加到该类中。Add a private member of type ExtensionDataObject to the class.

  4. 使用新的私有成员为该属性实现 get 和 set 方法。Implement get and set methods for the property using the new private member.

  5. DataContractAttribute 属性应用于该类。Apply the DataContractAttribute attribute to the class. 如有必要,将 NameNamespace 属性设置为适当的值。Set the Name and Namespace properties to appropriate values if necessary.

有关类型的版本控制的详细信息,请参阅 数据协定版本控制For more information about versioning of types, see Data Contract Versioning. 有关创建向前兼容的数据协定的信息,请参阅 向前兼容的数据协定For information about creating forward-compatible data contracts, see Forward-Compatible Data Contracts. 有关数据协定的详细信息,请参阅 使用数据协定For more information about data contracts, see Using Data Contracts.

属性

ExtensionData

获取或设置包含额外数据的结构。Gets or sets the structure that contains extra data.

适用于

另请参阅