Aracılığıyla paylaş


ServiceKnownTypeAttribute Sınıf

Tanım

Seri hale getirme veya seri durumdan çıkarma sırasında hizmet tarafından kullanılacak bilinen türleri belirtir.

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
Devralma
ServiceKnownTypeAttribute
Öznitelikler

Örnekler

Aşağıdaki örnek özniteliğini, özniteliğinin ServiceKnownTypeAttribute bir yöntem adı ve bildirim türü belirttiği bir arabirime uygular.

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

Alternatif olarak, özniteliğini eklenecek bilinen türü belirten arabirime uygulayın.

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

Açıklamalar

, ServiceKnownTypeAttribute Windows Communication Foundation (WCF) hizmet sözleşmeleri (hizmeti ve yöntemlerini tanımlayan arabirimler) oluştururken kullanılmak üzere tasarlanmıştır. Bilinen türler, serileştirme veya seri durumdan çıkarma gerçekleştiğinde nesne grafında bulunabilecek türlerdir. Bilinen türler hakkında daha fazla bilgi için bkz. Veri Sözleşmesi Bilinen Türleri.

özelliğini kullanmak MethodName için, bir tür dizisi (her biri bilinen bir tür) döndüren bir yöntem (veya yöntemler) içeren bir sınıf oluşturun. özniteliğini uygularken, methodName türünü tür listesini döndüren bir yöntemin adına ayarlayın ve öğesini declaringType yöntemini içeren türe ayarlayın. yöntemi arabirimini uygulayan IEnumerable<T> bir tür döndürmelidir. yöntemi, türünde ICustomAttributeProviderbir parametre de içermelidir.

Ayrıca özniteliğini bir arabirime, sınıfa veya yönteme her seferinde yeni bir bilinen tür belirterek birkaç kez uygulayabilirsiniz.

Uyarı

Microsoft Visual Basic veya C# kodunuzda sözcüğünü ServiceKnownType daha uzun ServiceKnownTypeAttributeyerine kullanabilirsiniz.

Oluşturucular

Name Description
ServiceKnownTypeAttribute(String, Type)

Sınıfının yeni bir örneğini ServiceKnownTypeAttribute , bilinen türleri döndüren bir yöntemin adıyla ve bilinen türleri döndüren yöntemi (veya yöntemleri) içeren türüyle başlatır.

ServiceKnownTypeAttribute(String)

sınıfının yeni bir örneğini ServiceKnownTypeAttribute başlatır ve bilinen türleri döndüren bir yöntemin adını belirtir.

ServiceKnownTypeAttribute(Type)

Belirtilen bilinen türe sahip sınıfın ServiceKnownTypeAttribute yeni bir örneğini başlatır.

Özellikler

Name Description
DeclaringType

Bilinen türleri döndüren yöntemleri içeren türü alır.

MethodName

Bilinen türlerin koleksiyonunu döndüren bir yöntemin adını alır.

Type

Nesne grafında yer alan bilinen tür.

TypeId

Türetilmiş bir sınıfta uygulandığında, bu Attributeiçin benzersiz bir tanımlayıcı alır.

(Devralındığı yer: Attribute)

Yöntemler

Name Description
Equals(Object)

Bu örneğin belirtilen bir nesneye eşit olup olmadığını gösteren bir değer döndürür.

(Devralındığı yer: Attribute)
GetHashCode()

Bu örneğin karma kodunu döndürür.

(Devralındığı yer: Attribute)
GetType()

Geçerli örneğin Type alır.

(Devralındığı yer: Object)
IsDefaultAttribute()

Türetilmiş bir sınıfta geçersiz kılındığında, bu örneğin değerinin türetilmiş sınıf için varsayılan değer olup olmadığını gösterir.

(Devralındığı yer: Attribute)
Match(Object)

Türetilmiş bir sınıfta geçersiz kılındığında, bu örneğin belirtilen bir nesneye eşit olup olmadığını gösteren bir değer döndürür.

(Devralındığı yer: Attribute)
MemberwiseClone()

Geçerli Objectbasit bir kopyasını oluşturur.

(Devralındığı yer: Object)
ToString()

Geçerli nesneyi temsil eden bir dize döndürür.

(Devralındığı yer: Object)

Belirtik Arabirim Kullanımları

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

Bir ad kümesini karşılık gelen bir dağıtma tanımlayıcısı kümesine eşler.

(Devralındığı yer: Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Bir arabirimin tür bilgilerini almak için kullanılabilecek bir nesnenin tür bilgilerini alır.

(Devralındığı yer: Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Bir nesnenin sağladığı tür bilgisi arabirimlerinin sayısını alır (0 ya da 1).

(Devralındığı yer: Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Bir nesne tarafından kullanıma sunulan özelliklere ve yöntemlere erişim sağlar.

(Devralındığı yer: Attribute)

Şunlara uygulanır

Ayrıca bkz.