CollectionDataContractAttribute 類別

定義

當套用至集合型別時,就能夠自訂集合 Item 項目的規格。 這個屬性只能套用至由 DataContractSerializer 識別為有效、可序列化集合的型別。

public ref class CollectionDataContractAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, AllowMultiple=false, Inherited=false)]
public sealed class CollectionDataContractAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, AllowMultiple=false, Inherited=false)>]
type CollectionDataContractAttribute = class
    inherit Attribute
Public NotInheritable Class CollectionDataContractAttribute
Inherits Attribute
繼承
CollectionDataContractAttribute
屬性

範例

下列範例會將 CollectionDataContractAttribute 套用至繼承自 List<T> 類別的類別。 這段程式碼會將 NameItemName 屬性設定為自訂值。

[CollectionDataContract(Name = "Custom{0}List", ItemName = "CustomItem")]
public class CustomList<T> : List<T>
{
    public CustomList()
        : base()
    {
    }

    public CustomList(T[] items)
        : base()
    {
        foreach (T item in items)
        {
            Add(item);
        }
    }
}
<CollectionDataContract(Name := "Custom{0}List", ItemName := "CustomItem")> _
Public Class CustomList(Of T)
    Inherits List(Of T)
    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal items() As T)
        MyBase.New()
        For Each item As T In items
            Add(item)
        Next item
    End Sub
End Class

ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 用來產生用戶端的程式碼時,程式碼會類似下列範例。 請注意,類別的名稱以及 ItemName 都已經變更過。 當使用泛型時,型別參數名稱會用來建立結果型別名稱。

// This is the generated code. Note that the class is renamed to "CustomBookList",
// and the ItemName is set to "CustomItem".
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.CollectionDataContractAttribute(ItemName = "CustomItem")]
public class CustomBookList : System.Collections.Generic.List<Microsoft.Security.Samples.Book>
{
}
' This is the generated code. Note that the class is renamed to "CustomBookList", 
' and the ItemName is set to "CustomItem".
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0"), System.Runtime.Serialization.CollectionDataContractAttribute(ItemName := "CustomItem")> _
Public Class CustomBookList
    Inherits System.Collections.Generic.List(Of Microsoft.Security.Samples.Book)
End Class

備註

CollectionDataContractAttribute這是為了在處理來自非 WCF 提供者的資料時簡化互通性,以及控制序列化實例的確切形狀。 為此,ItemName 屬性能夠讓您控制集合內重複項目的名稱。 這個方法在提供者不使用 XML 項目型別名稱做為陣列項目名稱時特別有用,例如,當提供者使用 "String" 當做項目型別名稱,而不是使用 XSD 型別名稱 "string" 的情況。

CollectionDataContractAttribute 也可以用來搭配字典型別,以便處理索引集合。 字典型別是會實作 IDictionaryIDictionary<TKey,TValue> 介面的類別,例如 Dictionary<TKey,TValue>。 使用 KeyNameValueName 屬性,即可在使用 Dictionary<TKey,TValue> 類別時設定自訂名稱。

如需使用 DataContractSerializer 的詳細資訊,請參閱 使用資料合約

建構函式

CollectionDataContractAttribute()

初始化 CollectionDataContractAttribute 類別的新執行個體。

屬性

IsItemNameSetExplicitly

確認是否已明確設定 ItemName

IsKeyNameSetExplicitly

確認是否已明確設定 KeyName

IsNameSetExplicitly

確認是否已明確設定 Name

IsNamespaceSetExplicitly

確認是否已明確設定 Namespace

IsReference

取得或設定值,這個值表示是否要保留物件參考資料。

IsReferenceSetExplicitly

取得是否已明確設定參考。

IsValueNameSetExplicitly

確認是否已明確設定 ValueName

ItemName

取得或設定集合項目的自訂名稱。

KeyName

取得或設定字典索引鍵名稱的自訂名稱。

Name

取得或設定集合型別的資料合約名稱。

Namespace

取得或設定資料合約的命名空間。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
ValueName

取得或設定字典值名稱的自訂名稱。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱