Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to put DataMemberAttribute on interface member?

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

like image 378
Adibe7 Avatar asked Jan 25 '11 08:01

Adibe7


People also ask

What is DataMemberAttribute?

Data Member are the fields or properties of your Data Contract class. You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member.

What is Datacontract and DataMember in C#?

A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.

What is Datacontract attribute?

[DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service.

What is DataMember in VB net?

The DataMember property sets or returns a string value that contains the name of the data member that will be retrieved from the object referenced by the DataSource property.


2 Answers

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.

like image 119
vc 74 Avatar answered Oct 20 '22 01:10

vc 74


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
like image 41
Jonathan Avatar answered Oct 20 '22 00:10

Jonathan