Windows Communication Foundation (WCF) uses a serialization engine called the Data Contract Serializer by default to serialize and deserialize data (convert it to and from XML).

data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.

Serializable标记大家都很熟悉,它是XmlSerializer的标记,在WCF中其实很少用这个标记,因为我们WCF用的是DataContractSerializer,对应的标记也是DataContract

Primitive类型默认是直接可被序列化的,自定义的类型要用DataContract序列化处理,WCF用DataContactSerializer。在WCF中一旦一个类被标记为DataContract,那么只有标记为DataMember的字段/属性才会被序列化。

All .NET Framework primitive types, such as integers and strings, as well as certain types treated as primitives, such as DateTime and XmlElement, can be serialized with no other preparation and are considered as having default data contracts.

DataContract也可以有继承,但是child和parent都要有[DataContract]attribute:

[DataContract]
public class ConsoleData{
  [DataMember]
  public String Description{ get;set;}
}
[DataContract]
public class SomeData:ConsoleData{
  [DataMember]
  public int Volume{ get;set;}
}
但是在使用方面,这样还不足够,在用parent class作为参数的方法上,它无法识别child class object,所以要在parent DataContract上register child class:
[DataContract]
[KnownType(typeof(SomeData)]
public class ConsoleData{
[DataMember]
public String Description{ get;set;}
}
 

C# DataContract DataMember的更多相关文章

  1. Web API DataContract DataMember Serializable简单解释

    首先看一下DataContract这个类契约: Web API/WCF 中类一旦标记了DataContract 属性,那么类中的属性只有被标记为DataMember属性才会被序列化,也就是说一个类的属 ...

  2. Configure JSON.NET to ignore DataContract/DataMember attributes

    https://stackoverflow.com/questions/11055225/configure-json-net-to-ignore-datacontract-datamember-at ...

  3. DataContract] [DataMember] 程序集引用

    引用,右键->添加引用->System.Runtime.Serialization 添加

  4. C# 解析JSON的几种办法

    欲成为海洋大师,必知晓海中每一滴水的真名. 刚开始只是想找一个转换JSON数组的方法,结果在MSDN翻到一大把. 搜索过程中免不了碰到一大堆名词:WCF => DataContract => ...

  5. C#中JSON序列化和反序列化

    有一段时间没有到博客园写技术博客了,不过每天逛逛博客园中大牛的博客还是有的,学无止境…… 最近在写些调用他人接口的程序,用到了大量的JSON.XML序列化和反序列化,今天就来总结下json的序列化和反 ...

  6. 在Application中集成Microsoft Translator服务之获取访问令牌

    我在这里画了一张图来展示业务逻辑 在我们调用microsoft translator server之前需要获得令牌,而且这个令牌的有效期为10分钟.下表列出所需的参数和对于的说明 参数 描述 clie ...

  7. WCF序列化

    在WCF中,提供了专门用来序列化和反序列操作的类,该类就是DataContractSerializer类.一般而言,WCF会自动选择使用DataContractSerializer来对可序列话数据契约 ...

  8. (转)c# 解析JSON的几种办法

    来自:http://blog.csdn.net/gaofang2009/article/details/6073029 欲成为海洋大师,必知晓海中每一滴水的真名. 刚开始只是想找一个转换JSON数组的 ...

  9. 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化

    [源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

随机推荐

  1. python 使用*args 和**kwargs

    def fun_var_args(farg, *args): print "arg:", farg for value in args: print "another a ...

  2. Shell基础:变量类型 & 运算符

    Shell变量 Shell支持三种类型的变量 用户自定义变量:用户自定义的变量,变量名以英文字母或下划线开头,区分大小写. 位置变量:根据位置传递参数给脚本的变量,默认支持9个位置变量 $1,$2,$ ...

  3. HDUOJ-------2719The Seven Percent Solution

    The Seven Percent Solution Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  4. ABAP版连连看

    网上看到的,感觉不错,借来装13... *&---------------------------------------------------------------------* *&a ...

  5. display和visibility的区别

    一.display和visibility的相同与不同点 1.相同点:display和visibility都有讲元素隐藏的意思 2.不同点:display是元素隐藏,隐藏的元素不占文档流 而visibi ...

  6. js 获得每周周日到周一日期

    //得到每周的第一天(周日)function getFirstDateOfWeek(theDate){ var firstDateOfWeek; theDate.setDate(theDate.get ...

  7. iOS8 针对开发者所拥有的新特性汇总如下

    iOS8 针对开发者所拥有的新特性汇总如下 1.支持第三方键盘 2.自带网页翻译功能(即在线翻译) 3.指纹识别功能开放:第三方软件可以调用 4.Safari浏览器可直接添加新的插件. 5.可以把一个 ...

  8. bzoj 1965: [Ahoi2005]SHUFFLE 洗牌

    #include<cstdio> #include<cstring> #include<iostream> #define ll long long using n ...

  9. bzoj 1821: [JSOI2010]Group 部落划分 Group

    #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #def ...

  10. C# 子窗体点击按钮产生的新子窗体放在父窗体里

    情景展示: 父窗体Form1,左边是按钮,右边是panel(放置子窗体) 父窗体点击按钮,在panel显示第一个子窗体AA, AA有个按钮,点击按钮,是第二个子窗体ZZ, 怎样将AA的子窗体ZZ也显示 ...