IExtensibleDataObject 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供資料結構,以便儲存 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 屬性來存取。 透過資料進行接收、處理以及傳回的來回作業,額外資料會原封不動地傳回原始的傳送者。 這種方式可以用來儲存接收自合約未來版本的資料。 如果您不實作這個介面,任何額外的資料都會在來回作業期間遭到忽略並且捨棄。
為了使用這個版本控制功能,此時要
在類別中實作 IExtensibleDataObject 介面。
將 ExtensionData 屬性新增至型別。
將 ExtensionDataObject 型別的私用成員新增至類別。
使用新的私用成員來實作屬性的取得與設定方法。
將 DataContractAttribute 屬性套用至該類別。 將 Name 和 Namespace 屬性設定為適當的值 (如有必要)。
如需類型版本設定的詳細資訊,請參閱 資料合約版本控制。 如需建立向前相容資料合約的詳細資訊,請參閱 順向相容資料合約。 如需資料合約的詳細資訊,請參閱 使用資料合約。
屬性
ExtensionData |
取得或設定包含額外資料的結構。 |