序列化是将一个对象转换成字节流以达到将其长期保存在内存、数据库或文件中的处理过程。它的主要目的是保存对象的状态以便以后需要的时候使用。与其相反的过程叫做反序列化。
序列化一个对象
为了序列化一个对象,我们需要一个被序列化的对象,一个容纳被序列化了的对象的(字节)流和一个格式化器。进行序列化之前我们先看看System.Runtime.Serialization名字空间。ISerializable接口允许我们使任何类成为可序列化的类。
如果我们给自己写的类标识[Serializable]特性,我们就能将这些类序列化。除非类的成员标记了[NonSerializable],序列化会将类中的所有成员都序列化。
序列化的类型
二进制(流)序列化
SOAP序列化
XML序列化
二进制(流)序列化:
二进制(流)序列化是一种将数据写到输出流,以使它能够用来自动重构成相应对象的机制。二进制,其名字就暗示它的必要信息是保存在存储介质上,而这些必要信息要求创建一个对象的精确的二进制副本。在二进制(流)序列化中,整个对象的状态都被保存起来,而XML序列化只有部分数据被保存起来。为了使用序列化,我们需要引入System.Runtime.Serialization.Formatters.Binary名字空间. 下面的代码使用BinaryFormatter类序列化.NET中的string类型的对象。
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace SerializationTest { class Program { static void Main(string[] args) { //Serialization of String Object string strobj = "test string for serialization"; FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write , FileShare.None); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, strobj); stream.Close(); //Deserialization of String Object FileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read , FileShare.Read ); string readdata = (string)formatter.Deserialize(readstream); readstream.Close(); Console.WriteLine(readdata); Console.ReadLine(); } } }
SOAP序列化:
SOAP协议是一个在异构的应用程序之间进行信息交互的理想的选择。我们需要在应用程序中添加System.Runtime.Serialization.Formatters.Soap名字空间以便在.Net中使用SOAP序列化。SOAP序列化的主要优势在于可移植性。SoapFormatter把对象序列化成SOAP消息或解析SOAP消息并重构被序列化的对象。下面的代码在.Net中使用SoapFormatter类序列化string类的对象。
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Soap ; namespace SerializationTest { class Program { static void Main(string[] args) { //Serialization of String Object string strobj = "test string for serialization"; FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write , FileShare.None); SoapFormatter formatter = new SoapFormatter(); formatter.Serialize(stream, strobj); stream.Close(); //Deserialization of String Object FileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read , FileShare.Read ); string readdata = (string)formatter.Deserialize(readstream); readstream.Close(); Console.WriteLine(readdata); Console.ReadLine(); } } }
XML序列化:
根据MSDN的描述,“XML序列化将一个对象或参数的公开字段和属性以及方法的返回值转换(序列化)成遵循XSD文档标准的XML流。因为XML是一个开放的标准,XML能被任何需要的程序处理,而不管在什么平台下,因此XML序列化被用到带有公开的属性和字段的强类型类中,它的这些发生和字段被转换成序列化的格式(在这里是XML)存储或传输。”
我们必须添加System.XML.Serialization引用以使用XML序列化。使用XML序列化的基础是XmlSerializer。下面的代码是在.Net中使用XmlSerializer类序列化string对象。
using System; using System.IO; using System.Xml.Serialization; namespace SerializationTest { class Program { static void Main(string[] args) { //Serialization of String Object string strobj = "test string for serialization"; FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write , FileShare.None); XmlSerializer xmlserializer = new XmlSerializer(typeof(string)); xmlserializer.Serialize(stream, strobj); stream.Close(); //Deserialization of String Object FileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read , FileShare.Read ); string readdata = (string)xmlserializer.Deserialize(readstream); readstream.Close(); Console.WriteLine(readdata); Console.ReadLine(); } } }
什么是格式化器?
一个格式化器用来确定一个对象的序列格式。它们目的是在网络上传输一个对象之前将其序列化成合适的格式。它们提供IFormatter接口。在.NET里提供了两个格式化类:BinaryFormatter和SoapFormatter,它们都继承了IFormatter接口。
使用序列化
Serialization allows developers to save the state of an object and reconstruct the object when needed, while well supporting object storage and data exchange. Through serialization, developers can use Web services to send objects to remote applications, transfer objects from one domain to another, transfer an object in XML format and pass a firewall, or maintain security or users between applications. specific information, etc.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
