ServiceKnownTypeAttribute 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 ServiceKnownTypeAttribute 類別的新執行個體。
多載
ServiceKnownTypeAttribute(String) |
初始化 ServiceKnownTypeAttribute 類別的新執行個體,並指定傳回已知型別的方法名稱。 |
ServiceKnownTypeAttribute(Type) |
使用指定的已知型別來初始化 ServiceKnownTypeAttribute 類別的新執行個體。 |
ServiceKnownTypeAttribute(String, Type) |
使用傳回已知型別的方法名稱和包含傳回已知型別之方法的型別,初始化 ServiceKnownTypeAttribute 類別的新執行個體。 |
ServiceKnownTypeAttribute(String)
初始化 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 類別的新執行個體。
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 類別的新執行個體。
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