语言

ServiceKnownTypeAttribute 构造函数

定义

初始化 ServiceKnownTypeAttribute 类的新实例。

重载

名称 说明
ServiceKnownTypeAttribute(String)

初始化类的新实例 ServiceKnownTypeAttribute ,并指定返回已知类型的方法的名称。

ServiceKnownTypeAttribute(Type)

使用指定的已知类型初始化类的新实例 ServiceKnownTypeAttribute 。

ServiceKnownTypeAttribute(String, Type)

使用返回已知类型和包含返回已知类型的方法(或方法)的名称初始化类的新实例 ServiceKnownTypeAttribute 。

ServiceKnownTypeAttribute(String)

Source:
ServiceKnownTypeAttribute.cs
Source:
ServiceKnownTypeAttribute.cs
Source:
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)

Source:
ServiceKnownTypeAttribute.cs
Source:
ServiceKnownTypeAttribute.cs
Source:
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)

Source:
ServiceKnownTypeAttribute.cs
Source:
ServiceKnownTypeAttribute.cs
Source:
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

另请参阅

适用于