Serialization in .NET

xiaoxiao2021-03-06  33

Serialization in .NET is divided into three categories: binary, SOAP, XML. The three sequence / reverse sequence-based classes are system.runtime.serialization.formatters.binary.binaryformatter, system.runtime.serialization.formatters.soap.SoApFormatter and System.xml.Serialization.xmlSerializer.

BinaryFormatter is used for ViewState Save and recovery in ASP.NET, and the default formatter of TCPChannel in Remoting, soapFormatter is used in Remoting's default formatter, and XMLSerializer is used in .NET Web Service. Although SOAPFormatter is also a character stream in XML format, it contains some of the data needed for the SOAP protocol, so it is much better than the amount of data generated than XMLSerializer, which is why Web Service is better than the Remoting efficiency than using HttpChannel SOAPFormatter.

When serialization using SOAPFORMATTER and XMLSerializer, the class needs to provide the PUBLIC's non-refined constructor, and BinaryFormatter has no such need.

BINNARYFORMATTER defaults all fields of the sequence class instance, while SOAPFormatter and XMLSerializer defaults to all public fields and properties of the instance of the class instance, and attributes provide GET and SET methods at the same time.

BinaryFormatter can complete the type information of the serialized object, including Assembly, version number, etc. The type of SOAPFormatter is weak, although it is required to qualify the name, but actually serialization and reverse selecente objects can have a great difference, such as different Assembly, private fields have some differences. XMLSerializer requires only format compatibility, ie the XML anti-sequence of XML definitions that may be sered from Namespacea.classa into an instance of Namespaceb.classb.

Supporting the class of binnaryformatter needs to add SerializableAttribute to the control of the serialization process, there are four ways:

Add NonserializedAttribute for a field that does not require serialization.

Implement interface iSerializable, custom serialization and reverse sequence process.

Implement interface IDSerializationCallback.

Set custom Binder for instances of BinnaryFormatter.

Support for the class of SOAPFormatter, there are three ways to control the serialization process:

Add corresponding SOAPXXXATTRIBUTE for fields or processes, such as SoapignoreAttribute, SoapeElementAttribute, SoapattributeAttribute, and more.

Implement interface IDSERILZATIONCALLBACK.

Set custom Binder for instances of BinnaryFormatter.

Support for XMLSerializer classes, there are four ways for the control of the serialization process:

Add corresponding XMLXXXAttributes for fields or processes, such as XmlignoreAttribute, XmlelementAttribute, XMlattributeAttribute, and so on.

Implement interface IXMLSERIALIZABLE (not recommended).

Implement interface IDSERILZATIONCALLBACK.

Specify XMLTYPEMAPPING or XMLATTRIBUTEOVERRIDES when you create XMLSerializer. This article only gives an introduction, and refers to the .NET documentation on how to implement control of the serialization / reverse sequence process in your code.

转载请注明原文地址:https://www.9cbs.com/read-62700.html

New Post(0)