CollectionDataContractAttribute CollectionDataContractAttribute CollectionDataContractAttribute CollectionDataContractAttribute Class


當套用至集合型別時,就能夠自訂集合 Item 項目的規格。When applied to a collection type, enables custom specification of the collection item elements. 這個屬性只能套用至由 DataContractSerializer 識別為有效、可序列化集合的型別。This attribute can be applied only to types that are recognized by the DataContractSerializer as valid, serializable collections.

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


下列範例會將 CollectionDataContractAttribute 套用至繼承自 List<T> 類別的類別。The following example applies the CollectionDataContractAttribute to a class that inherits from the List<T> class. 這段程式碼會將 NameItemName 屬性設定為自訂值。The code sets the Name and ItemName properties to custom values.

[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)
<CollectionDataContract(Name := "Custom{0}List", ItemName := "CustomItem")> _
Public Class CustomList(Of T)
	Inherits List(Of T)
	Public Sub New()
	End Sub

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

ServiceModel Metadata Utility Tool (Svcutil.exe)是用來產生用戶端的程式碼,該程式碼類似下列的範例。When the ServiceModel Metadata Utility Tool (Svcutil.exe) is used to generate code for the client, the code resembles the following example. 請注意,類別的名稱以及 ItemName 都已經變更過。Notice that the name of the class is changed, as well as the ItemName. 當使用泛型時,型別參數名稱會用來建立結果型別名稱。When using generics, the type parameter name is used to create the resulting type name.

// 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", "")]
[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", ""), System.Runtime.Serialization.CollectionDataContractAttribute(ItemName := "CustomItem")> _
Public Class CustomBookList
	Inherits System.Collections.Generic.List(Of Microsoft.Security.Samples.Book)
End Class


CollectionDataContractAttribute旨在簡化互通性,使用非 WCF 提供者的資料時,並控制序列化的執行個體的確切圖形。The CollectionDataContractAttribute is intended to ease interoperability when working with data from non-WCF providers and to control the exact shape of serialized instances. 為此,ItemName 屬性能夠讓您控制集合內重複項目的名稱。To this end, the ItemName property enables you to control the names of the repeating items inside a collection. 這個方法在提供者不使用 XML 項目型別名稱做為陣列項目名稱時特別有用,例如,當提供者使用 "String" 當做項目型別名稱,而不是使用 XSD 型別名稱 "string" 的情況。This is especially useful when the provider does not use the XML element type name as the array item name, for example, if a provider uses "String" as an element type name instead of the XSD type name "string".

CollectionDataContractAttribute 也可以用來搭配字典型別,以便處理索引集合。The CollectionDataContractAttribute is also intended to be used with dictionary types to handle keyed collections. 字典型別是會實作 IDictionaryIDictionary<TKey,TValue> 介面的類別,例如 Dictionary<TKey,TValue>Dictionary types are classes that implement either the IDictionary or the IDictionary<TKey,TValue> interface, for example, the Dictionary<TKey,TValue>. 使用 KeyNameValueName 屬性,即可在使用 Dictionary<TKey,TValue> 類別時設定自訂名稱。Use the KeyName and ValueName properties to set custom names when using the Dictionary<TKey,TValue> class.

如需使用詳細資訊DataContractSerializer,請參閱 < Using Data ContractsFor more information about using the DataContractSerializer, see Using Data Contracts.


CollectionDataContractAttribute() CollectionDataContractAttribute() CollectionDataContractAttribute() CollectionDataContractAttribute()

初始化 CollectionDataContractAttribute 類別的新執行個體。Initializes a new instance of the CollectionDataContractAttribute class.


IsItemNameSetExplicitly IsItemNameSetExplicitly IsItemNameSetExplicitly IsItemNameSetExplicitly

取得是否已明確設定 ItemNameGets whether ItemName has been explicitly set.

IsKeyNameSetExplicitly IsKeyNameSetExplicitly IsKeyNameSetExplicitly IsKeyNameSetExplicitly

取得是否已明確設定 KeyNameGets whether KeyName has been explicitly set.

IsNameSetExplicitly IsNameSetExplicitly IsNameSetExplicitly IsNameSetExplicitly

取得是否已明確設定 NameGets whether Name has been explicitly set.

IsNamespaceSetExplicitly IsNamespaceSetExplicitly IsNamespaceSetExplicitly IsNamespaceSetExplicitly

取得是否已明確設定 NamespaceGets whether Namespace has been explicitly set.

IsReference IsReference IsReference IsReference

取得或設定值,這個值表示是否要保留物件參考資料。Gets or sets a value that indicates whether to preserve object reference data.

IsReferenceSetExplicitly IsReferenceSetExplicitly IsReferenceSetExplicitly IsReferenceSetExplicitly

取得是否已明確設定參考。Gets whether reference has been explicitly set.

IsValueNameSetExplicitly IsValueNameSetExplicitly IsValueNameSetExplicitly IsValueNameSetExplicitly

取得是否已明確設定 ValueNameGets whether ValueName has been explicitly set.

ItemName ItemName ItemName ItemName

取得或設定集合項目的自訂名稱。Gets or sets a custom name for a collection element.

KeyName KeyName KeyName KeyName

取得或設定字典索引鍵名稱的自訂名稱。Gets or sets the custom name for a dictionary key name.

Name Name Name Name

取得或設定集合型別的資料合約名稱。Gets or sets the data contract name for the collection type.

Namespace Namespace Namespace Namespace

取得或設定資料合約的命名空間。Gets or sets the namespace for the data contract.

TypeId TypeId TypeId TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)
ValueName ValueName ValueName ValueName

取得或設定字典值名稱的自訂名稱。Gets or sets the custom name for a dictionary value name.


Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)


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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)