8

What does it mean to put a DataMemberAttribute on an interface member? How does this affect derived classes?

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
Adibe7
  • 3,469
  • 7
  • 30
  • 36
  • 1
    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx – CD.. Jan 25 '11 at 08:50

3 Answers3

10

As shown in the following signature, the DataMember attribute is not inheritable

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

Therefore, it makes very little sense to decorate interface members with this attribute as you will have to decorate the implementing classes' members with this attribute too.

vc 74
  • 37,131
  • 7
  • 73
  • 89
1

In my case, I use this attributes with my WCF services. When I make an interface for a WCF Webservice I do it defining an interface in this way:

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

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

End Interface

As you can see, the clien of this service will receive a POCOCLient class. Then I need to decorate the POCOClient class with the attributes you're asking form in this way in order to let the class to be serialized properly and send vía WCF.

<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
Jonathan
  • 11,809
  • 5
  • 57
  • 91
-1

The [DataMember] attribute, when applied to a member of a type, specifies that the member is part of a data contract. When this attribute is applied to a field or a property explicitly, it specifies that the member value will be serialized by an DataContractSerializer object (taken from Article)

Steve
  • 50,173
  • 4
  • 32
  • 41