クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
.NET Framework 3.5
.NET Framework 3.5
System.ServiceModel 名前空間
すべて縮小/すべて展開 すべて縮小
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2008/.NET Framework 3.5

その他のバージョンについては、以下の情報を参照してください。
.NET Framework クラス ライブラリ
ServiceKnownTypeAttribute クラス

更新 : 2007 年 11 月

シリアル化または逆シリアル化するときに、サービスによって使用される既知の型を指定します。

名前空間 :  System.ServiceModel
アセンブリ :  System.ServiceModel (System.ServiceModel.dll 内)
Visual Basic (宣言)
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Method Or AttributeTargets.Interface, Inherited := True,  _
    AllowMultiple := True)> _
Public NotInheritable Class ServiceKnownTypeAttribute _
    Inherits Attribute
Visual Basic (使用法)
Dim instance As ServiceKnownTypeAttribute
C#
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Interface, Inherited = true, 
    AllowMultiple = true)]
public sealed class ServiceKnownTypeAttribute : Attribute
Visual C++
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Method|AttributeTargets::Interface, Inherited = true, 
    AllowMultiple = true)]
public ref class ServiceKnownTypeAttribute sealed : public Attribute
J#
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Interface, Inherited = true, AllowMultiple = true) */
public final class ServiceKnownTypeAttribute extends Attribute
JScript
public final class ServiceKnownTypeAttribute extends Attribute

ServiceKnownTypeAttribute は、Windows Communication Foundation (WCF) サービス コントラクト (サービスとそのメソッドを定義するインターフェイス) の作成時に使用することを目的としています。既知の型は、シリアル化または逆シリアル化が発生するときにオブジェクト グラフ内に存在可能な型です。既知の型の詳細については、「 既知のデータ コントラクト型」を参照してください。

MethodName プロパティを使用するには、それぞれが既知の型である型の配列を返すメソッドを含むクラスを作成します。属性を適用する場合は、methodName を型のリストを返すメソッドの名前に設定し、declaringType をメソッドを含む型に設定します。メソッドは、IEnumerable<(Of <(T>)>) インターフェイスを実装する型を返す必要があります。さらに、メソッドには、ICustomAttributeProvider 型のパラメータを含める必要があります。

属性は、インターフェイス、クラス、またはメソッドに複数回適用でき、適用するたびに新しい既知の型を指定できます。

ms554667.alert_note(ja-jp,VS.90).gifメモ :

Microsoft Visual Basic または C# のコードでは、ServiceKnownTypeAttribute の代わりに ServiceKnownType という短い語を使用できます。

次の例は、ServiceKnownTypeAttribute 属性がメソッド名と宣言型を指定するインターフェイスに、その属性を適用しています。

Visual Basic
' 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 

C#
// 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;
}

または、含める既知の型を指定する属性をインターフェイスに適用します。

Visual Basic
' 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 
C#
// 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();
}
System..::.Object
  System..::.Attribute
    System.ServiceModel..::.ServiceKnownTypeAttribute
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。

Windows Vista, Windows XP SP2, Windows Server 2003

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。

.NET Framework

サポート対象 : 3.5、3.0
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2009 Microsoft Corporation. All rights reserved. 使用条件 | 商標 | プライバシー
Page view tracker