ServiceKnownTypeAttribute クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
シリアル化または逆シリアル化するときに、サービスによって使用される既知の型を指定します。
public ref class ServiceKnownTypeAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=true)]
public sealed class ServiceKnownTypeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=true)>]
type ServiceKnownTypeAttribute = class
inherit Attribute
Public NotInheritable Class ServiceKnownTypeAttribute
Inherits Attribute
- 継承
- 属性
例
次の例では、属性が ServiceKnownTypeAttribute メソッド名と宣言型を指定するインターフェイスに属性を適用します。
// Define a service contract and apply the ServiceKnownTypeAttribute
// to specify types to include when generating client code.
// The types must have the DataContractAttribute and DataMemberAttribute
// applied to be serialized and deserialized. The attribute specifies the
// name of a method (GetKnownTypes) in a class (Helper) defined below.
[ServiceKnownType("GetKnownTypes", typeof(Helper))]
[ServiceContract()]
public interface ICatalog
{
// Any object type can be inserted into a Hashtable. The
// ServiceKnownTypeAttribute allows you to include those types
// with the client code.
[OperationContract]
Hashtable GetItems();
}
// This class has the method named GetKnownTypes that returns a generic IEnumerable.
static class Helper
{
public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
{
System.Collections.Generic.List<System.Type> knownTypes =
new System.Collections.Generic.List<System.Type>();
// Add any types to include here.
knownTypes.Add(typeof(Widget));
knownTypes.Add(typeof(Machine));
return knownTypes;
}
}
[DataContract()]
public class Widget
{
[DataMember]
public string Id;
[DataMember]
public string Catalog;
}
[DataContract()]
public class Machine : Widget
{
[DataMember]
public string Maker;
}
' Define a service contract and apply the ServiceKnownTypeAttribute
' to specify types to include when generating client code.
' The types must have the DataContractAttribute and DataMemberAttribute
' applied to be serialized and deserialized. The attribute specifies the
' name of a method (GetKnownTypes) in a class (Helper) defined below.
<ServiceKnownType("GetKnownTypes", GetType(Helper)), ServiceContract()> _
Public Interface ICalculator
' Any object type can be inserted into a Hashtable. The
' ServiceKnownTypeAttribute allows you to include those types
' with the client code.
<OperationContract()> _
Function GetItems() As Hashtable
End Interface
' This class has the method named GetKnownTypes that returns a generic IEnumerable.
Friend Class Helper
Public Shared Function GetKnownTypes(provider As ICustomAttributeProvider) _
As IEnumerable(of Type)
Dim knownTypes As List(Of Type) = New List(Of Type)
' Add any types to include here.
knownTypes.Add(GetType(Widget))
knownTypes.Add(GetType(Machine))
Return knownTypes
End Function
End Class
<DataContract()> _
Public Class Widget
<DataMember()> _
Public Id As String
<DataMember()> _
Public Catalog As String
End Class
<DataContract()> _
Public Class Machine
Inherits Widget
<DataMember()> _
Public Maker As String
End Class
または、含める既知の型を指定する属性をインターフェイスに適用します。
// Apply the ServiceKnownTypeAttribute to the
// interface specifying the type to include. Apply
// the attribute more than once if needed.
[ServiceKnownType(typeof(Widget))]
[ServiceKnownType(typeof(Machine))]
[ServiceContract()]
public interface ICatalog2
{
// Any object type can be inserted into a Hashtable. The
// ServiceKnownTypeAttribute allows you to include those types
// with the client code.
[OperationContract]
Hashtable GetItems();
}
' Apply the ServiceKnownTypeAttribute to the
' interface specifying the type to include. Apply the attribute
' more than once, if needed.
<ServiceKnownType(GetType(Widget)), ServiceKnownType(GetType(Machine)), _
ServiceContract()> _
Public Interface ICalculator2
' Any object type can be inserted into a Hashtable. The
' ServiceKnownTypeAttribute allows you to include those types
' with the client code.
<OperationContract()> _
Function GetItems() As Hashtable
End Interface
注釈
これはServiceKnownTypeAttribute、Windows Communication Foundation (WCF) サービス コントラクト (サービスとそのメソッドを定義するインターフェイス) を作成するときに使用することを目的としています。 既知の型 は、シリアル化または逆シリアル化が発生したときにオブジェクト グラフに存在する可能性がある型です。 既知の型の詳細については、「既知のデータ コントラクト型」を参照してください。
このプロパティを MethodName 使用するには、型の配列 (それぞれが既知の型) を返すメソッド (またはメソッド) を含むクラスを作成します。 属性を適用するときは、型の一覧を返すメソッドの名前を設定 methodName し、メソッドを declaringType 含む型に設定します。 メソッドは、インターフェイスを実装する型を IEnumerable<T> 返す必要があります。 このメソッドには、型 ICustomAttributeProviderのパラメーターも含める必要があります。
属性は、インターフェイス、クラス、またはメソッドに複数回適用でき、適用するたびに新しい既知の型を指定できます。
注意
長いServiceKnownTypeAttributeコードではなく、Microsoft Visual Basic または C# コードでこの単語ServiceKnownTypeを使用できます。
コンストラクター
| ServiceKnownTypeAttribute(String) |
ServiceKnownTypeAttribute クラスの新しいインスタンスを初期化し、既知の型を返すメソッドの名前を指定します。 |
| ServiceKnownTypeAttribute(String, Type) |
既知の型を返すメソッドの名前と、既知の型を返すメソッドを含む型を指定して、ServiceKnownTypeAttribute クラスの新しいインスタンスを初期化します。 |
| ServiceKnownTypeAttribute(Type) |
指定した既知の型を使用して ServiceKnownTypeAttribute クラスの新しいインスタンスを初期化します。 |
プロパティ
| DeclaringType |
既知の型を返すメソッドを含む型を取得します。 |
| MethodName |
既知の型のコレクションを返すメソッドの名前を取得します。 |
| Type |
オブジェクト グラフに含めることができる既知の型。 |
| TypeId |
派生クラスで実装されると、この Attribute の一意の識別子を取得します。 (継承元 Attribute) |
メソッド
| 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) |