2011-01-25 11 views

回答

9

如下面的簽名中顯示的數據成員屬性是不可繼承

[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field, Inherited = false, 
    AllowMultiple = false)] 
public sealed class DataMemberAttribute : Attribute 

因此,讓人很沒有意義的裝飾接口成員具有這種屬性,你將有裝飾的實現類'也有這個屬性的成員。

-1

[DataMember]屬性應用於類型的成員時,指定該成員是數據協定的一部分。當此屬性顯式應用於字段或屬性時,它指定成員值將由DataContractSerializer對象序列化(取自Article

1

在我的情況中,我將這個屬性用於我的WCF服務。當我做一個接口的WCF web服務我做定義這樣的接口:

Imports System.ServiceModel 
<ServiceContract()> 
Public Interface IClientContract 

    <OperationContract()> 
    Function GetClientList() As IList(Of POCOClients) 

End Interface 

正如你所看到的,這個服務的CLIEN將獲得POCOCLient類。然後,我需要用這種方式用你要求的屬性裝飾POCOClient類,以便讓該類正確序列化併發送víaWCF。

<DataContract()> 
<MetadataType(GetType(POCOAuthorizedkeys.POCOAuthorizedkeysMetaData))> 
Public Class POCOAuthorizedkeys 

    <DataMember()> 
    <DisplayName("Id")> 
    Public Property Id As Integer 
    <DataMember()> 
    <DisplayName("IdPackage")> 
    Public Property IdPackage As Integer 
    <DataMember()> 
    <DisplayName("AuthorizedKey")> 
    Public Property AuthorizedKey As String 
    <DataMember()> 
    <DisplayName("IdUnthrustedClient")> 
    Public Property IdUnthrustedClient As Nullable(Of Integer) 

End Class 
+1

那些不可靠的客戶端是最糟糕的.. – stuartd 2013-02-14 10:11:29