将DataMemberAttribute放在接口成员上意味着什么?

将DataMemberAttribute放在接口成员上意味着什么? 这对派生类有何影响?

如以下签名所示,DataMember属性不可inheritance

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

因此,使用此属性修饰接口成员几乎没有意义,因为您还必须使用此属性修饰实现类的成员。

就我而言,我将此属性与我的WCF服务一起使用。 当我为WCF Web服务创建一个接口时,我会以这种方式定义一个接口:

 Imports System.ServiceModel  Public Interface IClientContract  Function GetClientList() As IList(Of POCOClients) End Interface 

如您所见,此服务的客户将收到POCOCLient类。 然后我需要用这种方式用你要求表单的属性来装饰POCOClient类,以便让类正确地序列化并发送víaWCF。

   Public Class POCOAuthorizedkeys   Public Property Id As Integer   Public Property IdPackage As Integer   Public Property AuthorizedKey As String   Public Property IdUnthrustedClient As Nullable(Of Integer) End Class 

[DataMember]属性应用于类型成员时,指定该成员是数据协定的一部分。 当此属性显式应用于字段或属性时,它指定成员值将由DataContractSerializer对象序列化(取自文章 )