Bagikan melalui


ServiceKnownTypeAttribute Kelas

Definisi

Menentukan jenis yang diketahui untuk digunakan oleh layanan saat membuat serialisasi atau deserialisasi.

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
Warisan
ServiceKnownTypeAttribute
Atribut

Contoh

Contoh berikut menerapkan ServiceKnownTypeAttribute atribut ke antarmuka tempat atribut menentukan nama metode dan jenis deklarasikan.

// 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

Atau, terapkan atribut ke antarmuka yang menentukan jenis yang diketahui untuk disertakan.

// 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

Keterangan

ServiceKnownTypeAttribute ini ditujukan untuk digunakan saat membuat kontrak layanan Windows Communication Foundation (WCF) (antarmuka yang menentukan layanan dan metodenya). Jenis yang diketahui adalah jenis yang mungkin ada dalam grafik objek saat serialisasi atau deserialisasi terjadi. Untuk informasi selengkapnya tentang jenis yang diketahui, lihat Jenis Kontrak Data yang Diketahui.

Untuk menggunakan MethodName properti , buat kelas yang berisi metode (atau metode) yang mengembalikan array jenis (masing-masing menjadi jenis yang diketahui). Saat menerapkan atribut, atur methodName ke nama metode yang mengembalikan daftar jenis dan atur declaringType ke jenis yang berisi metode . Metode harus mengembalikan jenis yang mengimplementasikan IEnumerable<T> antarmuka. Metode ini juga harus menyertakan parameter jenis ICustomAttributeProvider.

Anda juga dapat menerapkan atribut beberapa kali ke antarmuka, kelas, atau metode, setiap kali menentukan jenis baru yang diketahui.

Nota

Anda dapat menggunakan kata ServiceKnownType dalam kode Microsoft Visual Basic atau C# Anda alih-alih lebih lama ServiceKnownTypeAttribute.

Konstruktor

Nama Deskripsi
ServiceKnownTypeAttribute(String, Type)

Menginisialisasi instans ServiceKnownTypeAttribute baru kelas dengan nama metode yang mengembalikan jenis yang diketahui dan jenis yang berisi metode (atau metode) yang mengembalikan jenis yang diketahui.

ServiceKnownTypeAttribute(String)

Menginisialisasi instans ServiceKnownTypeAttribute baru kelas dan menentukan nama metode yang mengembalikan jenis yang diketahui.

ServiceKnownTypeAttribute(Type)

Menginisialisasi instans ServiceKnownTypeAttribute baru kelas dengan jenis yang diketahui yang ditentukan.

Properti

Nama Deskripsi
DeclaringType

Mendapatkan jenis yang berisi metode yang mengembalikan jenis yang diketahui.

MethodName

Mendapatkan nama metode yang mengembalikan kumpulan jenis yang diketahui.

Type

Jenis yang diketahui yang dapat disertakan dalam grafik objek.

TypeId

Ketika diimplementasikan dalam kelas turunan, mendapatkan pengidentifikasi unik untuk Attributeini.

(Diperoleh dari Attribute)

Metode

Nama Deskripsi
Equals(Object)

Mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu.

(Diperoleh dari Attribute)
GetHashCode()

Mengembalikan kode hash untuk instans ini.

(Diperoleh dari Attribute)
GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
IsDefaultAttribute()

Ketika ditimpa dalam kelas turunan, menunjukkan apakah nilai instans ini adalah nilai default untuk kelas turunan.

(Diperoleh dari Attribute)
Match(Object)

Saat ditimpa dalam kelas turunan, mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu.

(Diperoleh dari Attribute)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Implementasi Antarmuka Eksplisit

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

Memetakan sekumpulan nama ke sekumpulan pengidentifikasi pengiriman yang sesuai.

(Diperoleh dari Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Mengambil informasi jenis untuk objek, yang dapat digunakan untuk mendapatkan informasi jenis untuk antarmuka.

(Diperoleh dari Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Mengambil jumlah antarmuka informasi jenis yang disediakan objek (baik 0 atau 1).

(Diperoleh dari Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Menyediakan akses ke properti dan metode yang diekspos oleh objek.

(Diperoleh dari Attribute)

Berlaku untuk

Lihat juga