CollectionDataContractAttribute 类

定义

当应用于集合类型时,可用来对集合项元素进行自定义指定。 此属性只能应用于被 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>。 使用 KeyName 类时,请使用 ValueNameDictionary<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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅