Reputation: 39
I have a question about the [DataContract]
attribute.
I have written my code like below: here I am not using [DataContract]
attribute for my test class.
class test
{
[Datamember]
public string Strproperty
{
get;
set;
}
[Datamemer]
public string Strproperty2
{
get;
set;
}
}
class checktotal:Iservice
{
public string testmethod(test obj)
{
return obj.Strproperty+Strproperty2;
}
}
For that I am sending data from client I am getting the values correctly.
Here is it necessary to use [DataContract]
attribute for that test class?
If I removed [Datamember]
for test class property is getting error while sending from client. But I am not getting any errors even if I am not using the [DataContract]
attribute.
Please give me a brief explanation with example so that I can understand when to give that attribute and when do not give that attribute.
Thanks, Satya Pratap.
Upvotes: 0
Views: 493
Reputation: 755227
As of .NET 3.5 Service Pack 1, you can omit (not use) the [DataContract]
and [DataMember]
attributes. If you do that, then the DataContractSerializer
in WCF will behave just like the XML serializer - it will serialize all public properties only.
I prefer to use [DataContract]
and [DataMember]
explicitly anyway - it gives me the opportunity to specify options (like the data contract's XML namespace, the order of the [DataMember]
) and it lets me e.g. also exclude certain properties from serialization.
As soon as you start using [DataMember]
on one property, then only those properties decorated with a [DataMember]
will be looked at for the WCF serialization.
Upvotes: 1
Reputation: 3297
The DataContractSerializer
can deal with classes that do not have the DataContract
attribute if they provide a default constructor. See the MSDN documentation for more details.
Upvotes: 1