語言

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 屬性套用至 類別。 視需要將 Name 和 Namespace 屬性設定為適當的值。

如需類型版本設定的詳細資訊,請參閱 數據合約版本控制。 如需有關建立向前相容數據合約的資訊,請參閱 Forward-Compatible 數據合約。 如需資料合約的詳細資訊,請參閱使用資料合約。

屬性

名稱 Description
ExtensionData

取得或設定包含額外資料的結構。

適用於

另請參閱