語言

ServiceKnownTypeAttribute 建構函式

定義

初始化 ServiceKnownTypeAttribute 類別的新執行個體。

多載

名稱 Description
ServiceKnownTypeAttribute(String)

初始化該類別的新實例 ServiceKnownTypeAttribute ,並指定回傳已知型別的方法名稱。

ServiceKnownTypeAttribute(Type)

初始化一個以指定已知型別的新類別實例 ServiceKnownTypeAttribute 。

ServiceKnownTypeAttribute(String, Type)

初始化一個新的類別實例 ServiceKnownTypeAttribute ,名稱包含回傳已知型別的方法名稱,以及包含回傳已知型別的方法(或多個方法)的型別。

ServiceKnownTypeAttribute(String)

來源:
ServiceKnownTypeAttribute.cs
來源:
ServiceKnownTypeAttribute.cs
來源:
ServiceKnownTypeAttribute.cs

初始化該類別的新實例 ServiceKnownTypeAttribute ,並指定回傳已知型別的方法名稱。

public:
 ServiceKnownTypeAttribute(System::String ^ methodName);
public ServiceKnownTypeAttribute(string methodName);
new System.ServiceModel.ServiceKnownTypeAttribute : string -> System.ServiceModel.ServiceKnownTypeAttribute
Public Sub New (methodName As String)

參數

methodName
String

一個回傳已知型別的方法名稱。

備註

當將 套 ServiceKnownTypeAttribute 用到 包含已知型別方法的類別時,請使用此建構子。

另請參閱

適用於

ServiceKnownTypeAttribute(Type)

來源:
ServiceKnownTypeAttribute.cs
來源:
ServiceKnownTypeAttribute.cs
來源:
ServiceKnownTypeAttribute.cs

初始化一個以指定已知型別的新類別實例 ServiceKnownTypeAttribute 。

public:
 ServiceKnownTypeAttribute(Type ^ type);
public ServiceKnownTypeAttribute(Type type);
new System.ServiceModel.ServiceKnownTypeAttribute : Type -> System.ServiceModel.ServiceKnownTypeAttribute
Public Sub New (type As Type)

參數

type
Type

指定一個已知型別,可用於服務定義的參數或回傳值中。

範例

以下範例將屬性 ServiceKnownTypeAttribute 應用於一個介面,該屬性指定要包含的類型。

// 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 可以多次套用到一個方法,每次應用程式都會命名一個已知類型,這些類型可能存在於該方法回傳的物件圖中。

適用於

ServiceKnownTypeAttribute(String, Type)

來源:
ServiceKnownTypeAttribute.cs
來源:
ServiceKnownTypeAttribute.cs
來源:
ServiceKnownTypeAttribute.cs

初始化一個新的類別實例 ServiceKnownTypeAttribute ,名稱包含回傳已知型別的方法名稱,以及包含回傳已知型別的方法(或多個方法)的型別。

public:
 ServiceKnownTypeAttribute(System::String ^ methodName, Type ^ declaringType);
public ServiceKnownTypeAttribute(string methodName, Type declaringType);
new System.ServiceModel.ServiceKnownTypeAttribute : string * Type -> System.ServiceModel.ServiceKnownTypeAttribute
Public Sub New (methodName As String, declaringType As Type)

參數

methodName
String

一個回傳已知型別的方法名稱。

declaringType
Type

能在其物件圖中使用已知型態的類型。

範例

以下範例將屬性 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

另請參閱

適用於