WCF technology Profiling 13: Known types during serialization (known type)

Source: Internet
Author: User
Tags abstract datetime serialization

DataContractSerializer hosts the serialization and deserialization operations of all data contract objects. In the previous article (Data Contract and Data Contract serializers (DataContractSerializer)), we talked about DataContractSerializer basic serialization rules How to control the number of DataContractSerializer serialized or deserialized objects, and how to save the object reference structure of the serialized object in the serialized XML. In this article, we will discuss an important topic in WCF serialization in detail: Known types (known type).

The serialization and deserialization under WCF solves the transformation of data between two states: Managed Type objects and XML. Because types define the data structure of an object, you must determine the type of the object in advance, whether for serialization or deserialization. Serialization or deserialization will fail if the object that is serialized or deserialized generates an unknown type. To ensure normal serialization and deserialization of DataContractSerializer, we need to add the "unknown" type to the DataContractSerializer "known" Type list.

An unknown type causes serialization to fail

. NET can be divided into two types: declaring type and True type. We advocate interface-oriented programming, the real type of objects often need to be at run time to determine, in programming often only need to specify the type of declaration type, such as the type implementation of the interface or abstract class. When we use an interface-based or abstract class-created DataContractSerializer to serialize an instance that implements the interface or inherits the abstract class, it is often impossible to serialize the object's true type because it is not recognized. For example, in the following code, we define 3 types, an interface, an abstract class, and a concrete class.

 1:namespace Artech.datacontractserializerdemos 
2: {
3:public interface Iorder
4: {
5:guid ID
6: {get; set;}
7:
8:datetime Date
9: {get; set;}
Ten:
11:string Customer
: {get; set;}
:
14:string shipaddress
: {get; set;}
16:}
:
: [DataContract]
19:public abstract class Orderbase:iorder
: {
: [DataMember]
22:pu Blic Guid ID
: {get; set;}
:
: [DataMember]
26:public DateTime Date
: {get; set;}
:
: [DataMember]
30:public string Customer
: {get; set;}
:
: [DataMember]
34:public string shipaddress
: {get; set;}
36:}

: [DataContract]
39:public class Order:orderbase
: {
: [DataMember]
42:public DOUBL E totalprice
: {get; set;}
44:}
:}

When we serialize an order object in the following way (note that the generic type is Iorder or orderbase), the serializationexception exception shown in Figure 1 is thrown, indicating that the order type is unrecognized.

For the definition of the:serialize<t> method, refer to the previous article in this series: "WCF Technology profiling 12: Data Contract (information Contract) and Data Contract serializer (DataContractSerializer)."

 1:order order = New Order ()
2: {
3:id = Guid.NewGuid (),
4:customer = "NCS",
5:date = Datetime.today,
6:shipaddress = "#328, Airport Rd, Industrial Park, Suzhou Jiangsu Province",
7:totalprice = 8888.88
8:};
9:
10:serialize<iorder> (Order, @ "E:\order.xml");
11://or
12:serialize<orderbase> (Order, @ "E:\order.xml");

Figure 1 Serialization exception caused by "unknown" type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.