ServiceKnownTypeAttribute Construtores

Definição

Inicializa uma nova instância da classe ServiceKnownTypeAttribute.

Sobrecargas

ServiceKnownTypeAttribute(String)

Inicializa uma nova instância da classe ServiceKnownTypeAttribute e especifica o nome de um método que retorna os tipos conhecidos.

ServiceKnownTypeAttribute(Type)

Inicializa uma nova instância da classe ServiceKnownTypeAttribute com o tipo conhecido especificado.

ServiceKnownTypeAttribute(String, Type)

Inicializa uma nova instância da classe ServiceKnownTypeAttribute com o nome de um método que retorna os tipos conhecidos e o tipo que contém o método (ou métodos) que retorna os tipos conhecidos.

ServiceKnownTypeAttribute(String)

Inicializa uma nova instância da classe ServiceKnownTypeAttribute e especifica o nome de um método que retorna os tipos conhecidos.

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

Parâmetros

methodName
String

O nome de um método que retorna os tipos conhecidos.

Comentários

Use esse construtor ao aplicar a ServiceKnownTypeAttribute uma classe que contém métodos que retornam tipos conhecidos.

Confira também

Aplica-se a

ServiceKnownTypeAttribute(Type)

Inicializa uma nova instância da classe ServiceKnownTypeAttribute com o tipo conhecido especificado.

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

Parâmetros

type
Type

Especifica um tipo conhecido que pode ser usado em um parâmetro ou valor retornado definido pelo serviço.

Exemplos

O exemplo a seguir aplica o ServiceKnownTypeAttribute atributo a uma interface em que o atributo especifica o tipo a ser incluído.

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

Comentários

Pode ServiceKnownTypeAttribute ser aplicado várias vezes a um método, com cada aplicativo nomeando um tipo conhecido diferente que pode estar presente no grafo de objeto retornado pelo método.

Aplica-se a

ServiceKnownTypeAttribute(String, Type)

Inicializa uma nova instância da classe ServiceKnownTypeAttribute com o nome de um método que retorna os tipos conhecidos e o tipo que contém o método (ou métodos) que retorna os tipos conhecidos.

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)

Parâmetros

methodName
String

O nome de um método que retorna os tipos conhecidos.

declaringType
Type

O tipo que pode usar os tipos conhecidos em seu grafo de objeto.

Exemplos

O exemplo a seguir aplica o ServiceKnownTypeAttribute atributo a uma interface em que o atributo especifica um nome de método e um tipo de declaração.

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

Confira também

Aplica-se a