.NetFramework 数据保存与传输之序列化对象
序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,就使得数据能够被轻松地存储和传输。
.NET Framework 提供两种序列化技术:
- 二进制序列化保持类型保真度,这对于在应用程序的不同调用之间保留对象的状态很有用。例如,通过将对象序列化到剪贴板,可在不同的应用程序之间共享对象。您可以将对象序列化到流、磁盘、内存和网络等等。远程处理使用序列化“通过值”在计算机或应用程序域之间传递对象。
- XML 序列化仅序列化公共属性和字段,且不保留类型保真度。当您希望提供或使用数据而不限制使用该数据的应用程序时,这很有用。由于 XML 是一个开放式标准,因此,对于通过 Web 共享数据,它是一个有吸引力的选择。SOAP 同样是一个开放式标准,这使它也成为一个颇具吸引力的选择。
This following was added on 2005/11/22:
WebService Vs. Remoting
The key difference between ASP.NET Web services and .NET Remoting is in how they serialize data into messages and the format they choose for metadata.
ASP.NET Web Services, XmlSerializer and XSD
ASP.NET Web services rely on the System.Xml.Serialization.XmlSerializer class to marshal data to and from SOAP messages at runtime.
.NET Remoting, IFormatter and the Common Language Runtime
.NET Remoting relies on the pluggable implementations of the IFormatter interface used by the System.Runtime.Serialization engine to marshal data to and from messages. There are two standard formatters, System.Runtime.Serialization.Formatters.Binary.BinaryFormatter and System.Runtime.Serialization.Formatters.Soap.SoapFormatter.
ASP.NET Web services favor the XML Schema type system, and provide a simple programming model with broad cross-platform reach. .NET Remoting favors the runtime type system, and provides a more complex programming model with much more limited reach. This essential difference is the primary factor in determining which technology to use.