CollectionDataContractAttribute クラス
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
コレクション型に適用すると、コレクション項目要素をカスタム指定できます。 この属性は、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 クラスから継承するクラスに対して List<T> を適用する例を次に示します。 このコードは、Name プロパティと ItemName プロパティをカスタム値に設定します。
[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 要素型名を配列項目名として使用していない場合 (プロバイダーが XSD 型名 "string" の代わりに要素型名として "String" を使用している場合など) に特に役に立ちます。
CollectionDataContractAttribute には、ディクショナリ型と共に使用して、キー付きのコレクションを処理するという目的もあります。 ディクショナリ型は、IDictionary インターフェイスまたは IDictionary<TKey,TValue> インターフェイスのいずれか (例 : Dictionary<TKey,TValue>) を実装するクラスです。 KeyName プロパティと ValueName プロパティを使用して、Dictionary<TKey,TValue> クラスを使用するときのカスタム名を設定します。
の使用の詳細については、「データ コントラクトのDataContractSerializer使用」を参照してください。
Collection |
CollectionDataContractAttribute クラスの新しいインスタンスを初期化します。 |
Is |
ItemName が明示的に設定されているかどうかを取得します。 |
Is |
KeyName が明示的に設定されているかどうかを取得します。 |
Is |
Name が明示的に設定されているかどうかを取得します。 |
Is |
Namespace が明示的に設定されているかどうかを取得します。 |
Is |
オブジェクト参照データを保持するかどうかを示す値を取得または設定します。 |
Is |
参照が明示的に設定されているかどうかを取得します。 |
Is |
ValueName が明示的に設定されているかどうかを取得します。 |
Item |
コレクション要素のカスタム名を取得または設定します。 |
Key |
ディクショナリ キー名のカスタム名を取得または設定します。 |
Name |
コレクション型のデータ コントラクト名を取得または設定します。 |
Namespace |
データ コントラクトの名前空間を取得または設定します。 |
Type |
派生クラスで実装されると、この Attribute の一意の識別子を取得します。 (継承元 Attribute) |
Value |
ディクショナリ値名のカスタム名を取得または設定します。 |
Equals(Object) |
このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。 (継承元 Attribute) |
Get |
このインスタンスのハッシュ コードを返します。 (継承元 Attribute) |
Get |
現在のインスタンスの Type を取得します。 (継承元 Object) |
Is |
派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。 (継承元 Attribute) |
Match(Object) |
派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (継承元 Attribute) |
Memberwise |
現在の Object の簡易コピーを作成します。 (継承元 Object) |
To |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
_Attribute. |
一連の名前を対応する一連のディスパッチ識別子に割り当てます。 (継承元 Attribute) |
_Attribute. |
オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。 (継承元 Attribute) |
_Attribute. |
オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 (継承元 Attribute) |
_Attribute. |
オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。 (継承元 Attribute) |
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。