• C# DataContract DataMember


    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:

    总结一下吧,WCF中应用各个标记时所作的序列化处理:

    1.          不给任何标记将会做XML映射,所有公有属性/字段都会被序列化
    2.          [Serializable]标记会将所有可读写字段序列化
    3.        [DataContract]和[DataMember]联合使用来标记被序列化的字段

    数据契约(DataContract)
    服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型。

    一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务端和客户端之间传送,如下所示。

          [DataContract]

         public class UserInfo

         {

              //….

        }
    ---------------------

  • 相关阅读:
    微软发布全球建筑物数据
    echarts 迁徙地图的实现
    React剖析之组件基础
    React剖析之状态管理
    日常小记微信小程序
    SpringCloud中集成OenFeign时设置请求服务超时时间
    SpringCloud中集成Hystrix后通过Dashboard实现图形化监控实现
    SpringCloud中集成Hystrix实现熔断(从实例入手)
    SpringCloud中集成Hystrix实现降级时通过FeignFallBack实现通配服务
    SpringCloud中集成OpenFeign实现服务调用
  • 原文地址:https://www.cnblogs.com/shiyh/p/10536778.html
Copyright © 2020-2023  润新知