IExtensibleDataObject IExtensibleDataObject IExtensibleDataObject IExtensibleDataObject Interface

定義

提供資料結構,以便儲存 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. 使用新的私用成員來實作屬性的取得與設定方法。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. 如需有關資料合約的詳細資訊,請參閱Using Data ContractsFor more information about data contracts, see Using Data Contracts.

屬性

ExtensionData ExtensionData ExtensionData ExtensionData

取得或設定包含額外資料的結構。Gets or sets the structure that contains extra data.

適用於

另請參閱