When to use DataContract and DataMember attributes?
When to use DataContract and DataMember attributes?
I am very confused about the DataContract
attribute in WCF. As per my knowledge it is used for serialization user defined type like classes. I write a one class which is expose at client side.
[DataContract]
public class Contact
{
[DataMember]
public int Roll { get; set; } [DataMember]
public string Name { get; set; } [DataMember]
public string Address { get; set; } [DataMember]
public int Age { get; set; }
}
It is working properly but when I remove DataContract
at class level as well as DataMember
it is also work properly. I can't understand that if it is working properly so why there is a need of DataContract
? Can any one tell me what is the actual use of DataContract
?
My service contract looks like this
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
Contact XmlData(string id);
}
解答:
Since a lot of programmers were overwhelmed with the [DataContract]
and [DataMember]
attributes, with .NET 3.5 SP1, Microsoft made the data contract serializer handle all classes - even without any of those attributes - much like the old XML serializer.
So as of .NET 3.5 SP1, you don't have to add data contract or data member attributes anymore - if you don't then the data contract serializer will serialize all public properties on your class, just like the XML serializer would.
HOWEVER: by not adding those attributes, you lose a lot of useful capabilities:
- without
[DataContract]
, you cannot define an XML namespace for your data to live in - without
[DataMember]
, you cannot serialize non-public properties or fields - without
[DataMember]
, you cannot define an order of serialization (Order=
) and the DCS will serialize all properties alphabetically - without
[DataMember]
, you cannot define a different name for your property (Name=
) - without
[DataMember]
, you cannot define things likeIsRequired=
or other useful attributes - without
[DataMember]
, you cannot leave out certain public properties - all public properties will be serialized by the DCS
So for a "quick'n'dirty" solution, leaving away the [DataContract]
and [DataMember]
attributes will work - but it's still a good idea to have them on your data classes - just to be more explicit about what you're doing, and to give yourself access to all those additional features that you don't get without them...
When to use DataContract and DataMember attributes?的更多相关文章
- Configure JSON.NET to ignore DataContract/DataMember attributes
https://stackoverflow.com/questions/11055225/configure-json-net-to-ignore-datacontract-datamember-at ...
- DataContract和DataMember的作用
数据契约(DataContract)里的DataMember特性 来源于网络,用于个人收集及备忘 数据契约(DataContract) 服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端 ...
- DataContract 和 DataMember
数据契约(DataContract) 服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被 ...
- [DataContract]和[DataMember]缺少引用
1.项目->右键->添加引用->找到System.Runtime.Serialization 2.代码中加上 Using System.Runtime.Serialization
- WCF Windows Service Using TopShelf and ServiceModelEx z
http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...
- 数据契约(DataContract)及序列化指定输出字段
服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务端和客户端之间传送,如下所 ...
- 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...
- Conversion between json and object
public static string ObjToJson<T>(T obj) { DataContractJsonSerializer serializer = new DataCon ...
- 使用ServiceStack构建Web服务
提到构建WebService服务,大家肯定第一个想到的是使用WCF,因为简单快捷嘛.首先要说明的是,本人对WCF不太了解,但是想快速建立一个WebService,于是看到了MSDN上的这一篇文章 Bu ...
随机推荐
- A Swift Tour(4) - Objects and Classes
Objects and Classes(对象和类) 用 class 关键字后面跟一个类名来创建一个class,在一个类中声明 常亮或变量,他存在于当前类的上下文,函数的方法是同样的 var numbe ...
- T-SQL基础 (子查询,连接查询,交叉查询,事务|| 笔记0807)
一: A.子查询: 1.select 字段名 from table where 字段名=(select 字段名 from table 条件) //只能做1个匹配 2.select 字段名 from ...
- ref与out之间的区别整理
ref和out都是C#中的关键字,所实现的功能也差不多,都是指定一个参数按照引用传递. 对于编译后的程序而言,它们之间没有任何区别,也就是说它们只有语法区别. 总结起来,他们有如下语法区别: 1.re ...
- 快速排序算法(C#实现)
想到了快速排序,于是自己就用C#实现了快速排序的算法: 快速排序的基本思想:分治法,即,分解,求解,组合 . 分解:在 无序区R[low..high]中任选一个记录作为基准(通常选第一个记录,并记为k ...
- SQL 查找存储过程及视图与自带函数
查找所有所有存储过程的名称及信息select * from sysobjectswhere type='P' 查看存储过程定义语句sp_helptext [存储过程名] 查看所有视图及信息select ...
- iOS--NSNetService和NSNetServiceBrowser(Bonjour网络编程)
Cocoa 网络框架: Cocoa 网络框架有三层,最底层的是基于 BSD socket库,然后是 Cocoa 中基于 C 的 CFNetwork,最上面一层是 Cocoa 中 Bonjour.通常我 ...
- Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...
- Java面向对象程序设计--与C++对比说明:系列3(Java 继承机制)
继承(inheritance)背后的核心思想是: bonus = b; } } Java没有像C++那样提供多继承机制,但提供了接口机制,在后面我们将详细探究接口机制的实现 ...
- RSA安全性问题
加密:C=Me(mod n) 解密:M=Cd(mod n) 安全性基础: 穷举法攻击: 1.攻击者设计一个M,C=Me(mod n) 2.d的个数至多有n-1个,尝试使用每个d破解,如果M’=Cd‘( ...
- spark-shell - 将结果保存成一个文件
sqlContext.sql(""" SELECT user_no,cust_id,oper_code FROM cui.operation_data_an ...