ServiceKnownTypeAttribute 類別

定義

指定在序列化或還原序列化時,服務所使用的已知型別。Specifies known types to be used by a service when serializing or deserializing.

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
type ServiceKnownTypeAttribute = class
    inherit Attribute
Public NotInheritable Class ServiceKnownTypeAttribute
Inherits Attribute
繼承
ServiceKnownTypeAttribute
屬性

範例

下列範例會將 ServiceKnownTypeAttribute 屬性套用至介面,其中屬性會指定方法名稱和宣告類型。The following example applies the ServiceKnownTypeAttribute attribute to an interface where the attribute specifies a method name and a declaring type.

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

或者,將此屬性套用在介面上,以指定要包含的已知型別。Alternatively, apply the attribute to the interface specifying the known type to include.

// 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)服務合約(定義服務及其方法的介面)。The ServiceKnownTypeAttribute is intended for use when creating Windows Communication Foundation (WCF) service contracts (interfaces that define the service and its methods). 已知型別是當序列化或還原序列化發生時,可能會出現在物件圖形中的類型。The known types are types that may be present in an object graph when serialization or deserialization occurs. 如需已知類型的詳細資訊,請參閱資料合約已知類型For more information about known types, see Data Contract Known Types.

若要使用 MethodName 屬性,請建立一個類別,其中包含傳回類型陣列的方法(或方法)(每個都是已知型別)。To use the MethodName property, create a class that contains a method (or methods) that returns an array of types (each being a known type). 套用屬性時,請將 methodName 設定為傳回類型清單的方法名稱,並將 declaringType 設定為包含方法的類型。When applying the attribute, set the methodName to the name of a method that returns the list of types and set the declaringType to the type that contains the method. 方法必須傳回實作為 IEnumerable<T> 介面的型別。The method must return a type that implements the IEnumerable<T> interface. 此方法也必須包含 ICustomAttributeProvider類型的參數。The method must also include a parameter of type ICustomAttributeProvider.

您亦可將此屬性套用在介面、類別或方法上數次,且每次指定一個新的已知型別。You can also apply the attribute several times to an interface, class, or method, each time specifying a new known type.

注意

您可以使用 Microsoft Visual Basic 或C#程式碼中的文字 ServiceKnownType,而不是較長的 ServiceKnownTypeAttributeYou can use the word ServiceKnownType in your Microsoft Visual Basic or C# code instead of the longer ServiceKnownTypeAttribute.

建構函式

ServiceKnownTypeAttribute(String)

初始化 ServiceKnownTypeAttribute 類別的新執行個體,並指定傳回已知型別的方法名稱。Initializes a new instance of the ServiceKnownTypeAttribute class and specifies the name of a method that returns the known types.

ServiceKnownTypeAttribute(String, Type)

使用傳回已知型別的方法名稱和包含傳回已知型別之方法的型別,初始化 ServiceKnownTypeAttribute 類別的新執行個體。Initializes a new instance of the ServiceKnownTypeAttribute class with the name of a method that returns the known types and the type that contains the method (or methods) that returns the known types.

ServiceKnownTypeAttribute(Type)

使用指定的已知型別來初始化 ServiceKnownTypeAttribute 類別的新執行個體。Initializes a new instance of the ServiceKnownTypeAttribute class with the specified known type.

屬性

DeclaringType

取得包含了傳回已知型別的方法的型別。Gets the type that contains the methods that return the known types.

MethodName

取得傳回已知型別之集合的方法名稱。Gets the name of a method that returns the collection of known types.

Type

可包含在物件圖形中的已知型別。The known type that can be included in an object graph.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回此執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

Gets the Type of the current instance.Gets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

Creates a shallow copy of the current Object.Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

Returns a string that represents the current object.Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於

另請參閱