ServiceKnownTypeAttribute Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Určuje známé typy, které má služba používat při serializaci nebo deserializaci.
public ref class ServiceKnownTypeAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=true)]
public sealed class ServiceKnownTypeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=true)>]
type ServiceKnownTypeAttribute = class
inherit Attribute
Public NotInheritable Class ServiceKnownTypeAttribute
Inherits Attribute
- Dědičnost
- Atributy
Příklady
Následující příklad použije ServiceKnownTypeAttribute atribut na rozhraní, kde atribut určuje název metody a deklarující typ.
// 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
Případně použijte atribut na rozhraní určující známý typ, který se má zahrnout.
// 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
Poznámky
Je ServiceKnownTypeAttribute určen k použití při vytváření kontraktů služeb WCF (Windows Communication Foundation) (rozhraní, která definují službu a její metody). Známé typy jsou typy, které mohou být přítomny v objektovém grafu při serializaci nebo deserializaci. Další informace o známých typech naleznete v tématu Data Contract Známé typy.
Chcete-li použít MethodName vlastnost, vytvořte třídu, která obsahuje metodu (nebo metody), která vrací pole typů (každý je známý typ). Při použití atributu nastavte methodName na název metody, která vrátí seznam typů a nastaví declaringType na typ, který obsahuje metodu. Metoda musí vrátit typ, který implementuje IEnumerable<T> rozhraní. Metoda musí obsahovat také parametr typu ICustomAttributeProvider.
Atribut můžete také použít několikrát u rozhraní, třídy nebo metody pokaždé, když zadáte nový známý typ.
Poznámka:
Místo delšího ServiceKnownTypeAttributekódu můžete použít slovo ServiceKnownType v kódu jazyka Microsoft Visual Basic nebo C#.
Konstruktory
| Name | Description |
|---|---|
| ServiceKnownTypeAttribute(String, Type) |
Inicializuje novou instanci ServiceKnownTypeAttribute třídy s názvem metody, která vrací známé typy a typ, který obsahuje metodu (nebo metody), která vrací známé typy. |
| ServiceKnownTypeAttribute(String) |
Inicializuje novou instanci ServiceKnownTypeAttribute třídy a určuje název metody, která vrací známé typy. |
| ServiceKnownTypeAttribute(Type) |
Inicializuje novou instanci ServiceKnownTypeAttribute třídy se zadaným známým typem. |
Vlastnosti
| Name | Description |
|---|---|
| DeclaringType |
Získá typ, který obsahuje metody, které vracejí známé typy. |
| MethodName |
Získá název metody, která vrací kolekci známých typů. |
| Type |
Známý typ, který lze zahrnout do grafu objektu. |
| TypeId |
Při implementaci v odvozené třídě získá jedinečný identifikátor pro tento Attribute. (Zděděno od Attribute) |
Metody
| Name | Description |
|---|---|
| Equals(Object) |
Vrátí hodnotu, která určuje, zda je tato instance rovna zadanému objektu. (Zděděno od Attribute) |
| GetHashCode() |
Vrátí kód hash pro tuto instanci. (Zděděno od Attribute) |
| GetType() |
Získá Type aktuální instance. (Zděděno od Object) |
| IsDefaultAttribute() |
Při přepsání v odvozené třídě určuje, zda hodnota této instance je výchozí hodnotou pro odvozenou třídu. (Zděděno od Attribute) |
| Match(Object) |
Při přepsání v odvozené třídě vrátí hodnotu, která určuje, zda se tato instance rovná zadanému objektu. (Zděděno od Attribute) |
| MemberwiseClone() |
Vytvoří mělkou kopii aktuálního Object. (Zděděno od Object) |
| ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |
Explicitní implementace rozhraní
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Mapuje sadu názvů na odpovídající sadu identifikátorů pro rozesílání. (Zděděno od Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Načte informace o typu objektu, který lze použít k získání informací o typu pro rozhraní. (Zděděno od Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Získá počet rozhraní typu informací, které objekt poskytuje (0 nebo 1). (Zděděno od Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Poskytuje přístup k vlastnostem a metodám vystaveným objektem. (Zděděno od Attribute) |