.NET 中的序列化Serialization in .NET

序列化是将对象状态转换为可保持或传输的形式的过程。Serialization is the process of converting the state of an object into a form that can be persisted or transported. 序列化的补集是反序列化,后者将流转换为对象。The complement of serialization is deserialization, which converts a stream into an object. 这些进程一起允许存储和传输数据。Together, these processes allow data to be stored and transferred.

.NET 具有以下序列化技术:.NET features the following serialization technologies:

  • 二进制序列化保存类型保真,这对于在不同的应用程序调用之间保留对象的状态非常有用。Binary serialization preserves type fidelity, which is useful for preserving the state of an object between different invocations of an application. 例如,通过将对象序列化到剪贴板,可在不同的应用程序之间共享对象。For example, you can share an object between different applications by serializing it to the Clipboard. 您可以将对象序列化到流、磁盘、内存和网络等。You can serialize an object to a stream, to a disk, to memory, over the network, and so forth. 远程处理使用序列化,“按值”在计算机或应用程序域之间传递对象。Remoting uses serialization to pass objects "by value" from one computer or application domain to another.

  • XML 和 SOAP 序列化只会序列化公共属性和字段,并且不会保留类型保真。XML and SOAP serialization serializes only public properties and fields and does not preserve type fidelity. 当您希望提供或使用数据而不限制使用该数据的应用程序时,这一点非常有用。This is useful when you want to provide or consume data without restricting the application that uses the data. 由于 XML 是开放式的标准,因此它对于通过 Web 共享数据来说是一个理想选择。Because XML is an open standard, it is an attractive choice for sharing data across the Web. SOAP 同样是开放式的标准,这使它也成为一个理想选择。SOAP is likewise an open standard, which makes it an attractive choice.

  • JSON 序列化只序列化公共属性,不会保留类型保真。JSON serialization serializes only public properties and does not preserve type fidelity. JSON 是一种开放式标准,是一种用于在 web 上共享数据的极具吸引力的选择。JSON is an open standard that is an attractive choice for sharing data across the web.

参考Reference

System.Runtime.Serialization
包含可用于序列化和反序列化对象的类。Contains classes that can be used for serializing and deserializing objects.

System.Xml.Serialization
包含可用于将对象序列化为 XML 格式的文档或流的类。Contains classes that can be used to serialize objects into XML format documents or streams.

System.Text.Json
包含可用于将对象序列化为 JSON 格式文档或流的类。Contains classes that can be used to serialize objects into JSON format documents or streams.