zoukankan      html  css  js  c++  java
  • 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:

    [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;}
    }
     
  • 相关阅读:
    HDU 1402 A * B Problem Plus FFT
    HDU 4609 3-idiots FFT
    Hihocoder #1527 : 快速乘法 DP
    Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo DP+矩阵快速幂加速
    Codeforces 8VC Venture Cup 2016
    FFT做题记录
    Hackrank Candies DP
    git submodule update --init --recursive
    慢慢长大
    protobuf
  • 原文地址:https://www.cnblogs.com/chayu3/p/3498575.html
Copyright © 2011-2022 走看看