WCF中DataContractAttribute 类

一、这个类的作用

使用提供的数据协定,将类型实例序列化和反序列化为 XML 流或文档。无法继承此类,(DataContractSerializer 用于序列化和反序列化在 Windows Communication Foundation (WCF) 消息中发送的数据)用于wcf数据传输中。

二、怎么使用这个类

1.通过将DataContractAttribute 属性 (Attribute) 应用于类,而将 DataMemberAttribute 属性 (Attribute) 应用于类成员,可以指定要序列化的属性 (Property) 和字段。

例子:

 1  // Set the Name and Namespace properties to new values.
 2     [DataContract(Name = "Customer", Namespace = "http://www.contoso.com")] //Customer对应数据库中表名
 3     class Person : IExtensibleDataObject
 4     {
 5         // To implement the IExtensibleDataObject interface, you must also
 6         // implement the ExtensionData property.
 7         private ExtensionDataObject extensionDataObjectValue;
 8         public ExtensionDataObject ExtensionData
 9         {
10             get
11             {
12                 return extensionDataObjectValue;
13             }
14             set
15             {
16                 extensionDataObjectValue = value;
17             }
18         }
19 
20         [DataMember(Name = "CustName")]
21         internal string Name;
22 
23         [DataMember(Name = "CustID")]
24         internal int ID;
25 
26         public Person(string newName, int newID)
27         {
28             Name = newName;
29             ID = newID;
30         }
31 
32     }

 

原文地址:https://www.cnblogs.com/lihongchen/p/3611419.html