ServiceKnownTypeAttribute ServiceKnownTypeAttribute ServiceKnownTypeAttribute ServiceKnownTypeAttribute Class

定义

指定序列化或反序列化时服务要使用的已知类型。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)]
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method | System.AttributeTargets.Interface, AllowMultiple=true, Inherited=true)]
public sealed class ServiceKnownTypeAttribute : Attribute
type ServiceKnownTypeAttribute = class
    inherit Attribute
Public NotInheritable Class ServiceKnownTypeAttribute
Inherits Attribute
继承
ServiceKnownTypeAttributeServiceKnownTypeAttributeServiceKnownTypeAttributeServiceKnownTypeAttribute
属性

示例

下面的示例将 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. 有关已知类型的详细信息,请参阅Data Contract Known TypesFor 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(String) ServiceKnownTypeAttribute(String) 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(String, Type) ServiceKnownTypeAttribute(String, Type) 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(Type) ServiceKnownTypeAttribute(Type) ServiceKnownTypeAttribute(Type)

使用指定的已知类型初始化 ServiceKnownTypeAttribute 类的新实例。Initializes a new instance of the ServiceKnownTypeAttribute class with the specified known type.

属性

DeclaringType DeclaringType DeclaringType DeclaringType

获取包含返回已知类型的方法的类型。Gets the type that contains the methods that return the known types.

MethodName MethodName MethodName MethodName

获取返回已知类型集合的方法的名称。Gets the name of a method that returns the collection of known types.

Type Type Type Type

可包含在对象图中的已知类型。The known type that can be included in an object graph.

TypeId TypeId TypeId TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

适用于

另请参阅