以下文章提供了 C# 序列化的概述。將物件實(shí)例轉(zhuǎn)換為資料流的過(guò)程稱為序列化,並且物件實(shí)例的狀態(tài)轉(zhuǎn)換為資料流,因?yàn)樗梢钥绮煌W(wǎng)路傳輸並持久保存在儲(chǔ)存位置中。這是序列化的一個(gè)優(yōu)點(diǎn),可以將轉(zhuǎn)換後的資料流以跨平臺(tái)相容的格式跨不同網(wǎng)路傳輸,並將轉(zhuǎn)換後的串流資料以持久或非持久物件狀態(tài)保存到儲(chǔ)存媒體中,以便可以在不同的網(wǎng)路上傳輸相同的副本。後來(lái)創(chuàng)建的。
C# 序列化物件的步驟
以下是C#序列化物件的步驟:
- 建立了一個(gè)流物件。
- 建立了一個(gè) BinaryFormatter 物件。
- 呼叫 Serialize( ) 方法。
C# 序列化的工作原理
- 每當(dāng)我們使用應(yīng)用程式時(shí),都需要將資料儲(chǔ)存在持久或非持久的媒體中,以便以後可以檢索相同的資料。這可以透過(guò)使用序列化的概念來(lái)實(shí)現(xiàn)。
- 將物件的實(shí)例轉(zhuǎn)換為位元組流,將物件的狀態(tài)移到檔案的記憶體或資料庫(kù)中的過(guò)程稱為序列化。
- 序列化對(duì)於將物件以相容的格式透過(guò)網(wǎng)路傳輸?shù)娇缙脚_(tái)至關(guān)重要。
- 也可以使用序列化來(lái)建立物件的克隆。
- 程式中必須包含 Runtime.Serialization 命名空間才能在 C# 中使用序列化。
- [ Serialized ] 屬性用於在 C# 中使類別可序列化。
演示 [ Serialized ] 類別的範(fàn)例類別:
代碼:
[Serializable] public class Check { public int code; public string name; }
- 類似地,如果我們想要讓類別的任何成員不可序列化,我們可以使用 [ NonSerialized() ] 屬性。
考慮下面的範(fàn)例類別來(lái)示範(fàn) [ NonSerialized() ] 屬性:
代碼:
[Serializable] public class Check { public int code; public string name; [NonSerialized()] Public double price; }
- C# 支援以下類型的序列化。
下面給出了 C# 支持的序列化類型:
1.二進(jìn)位序列化
- 所有序列化技術(shù)中最快的是二元序列化。
- 可以使用二進(jìn)位序列化將物件序列化為二進(jìn)位流。
- 當(dāng)使用二元序列化將物件序列化為輸出流時(shí),物件的身份就會(huì)保留。
- 系統(tǒng)。運(yùn)行時(shí)。系列化。格式化程序。程式中必須包含二進(jìn)位命名空間才能使用二進(jìn)位序列化。
2. SOAP 序列化
- 簡(jiǎn)單物件存取協(xié)定是 SOAP 的縮寫(xiě)。
- 如果我們必須將物件從一個(gè)應(yīng)用程式傳輸?shù)接僧愘|(zhì)架構(gòu)組成的另一應(yīng)用程序,我們會(huì)使用簡(jiǎn)單物件存取協(xié)定序列化。
- 可移植性是使用簡(jiǎn)單物件存取協(xié)定序列化的主要好處。
- 可以使用簡(jiǎn)單物件存取協(xié)定序列化以簡(jiǎn)單物件存取協(xié)定的形式序列化物件。
- 系統(tǒng)。運(yùn)行時(shí)。系列化。格式化程序。程式中必須包含 Soap 命名空間才能使用簡(jiǎn)單物件存取協(xié)定序列化。
3. XML 序列化
- 可以使用 XML 序列化將類別實(shí)例的公共成員序列化為 XML 流。
- 與二元序列化的速度相比,XML 序列化的速度非常慢。
- 透過(guò)使用 XML 序列化提供跨平臺(tái)支援。
- XML 序列化是基於文字的。
- XML 序列化易於閱讀。
- XML 序列化可以輕鬆編輯。
- 可以在 XmlAttribute 上設(shè)定屬性,以使用 XML 序列化來(lái)序列化該屬性。
考慮下面的程式碼來(lái)示範(fàn) XmlAttribute 的使用:
代碼:
[XmlAttribute("Name")] public string Name { get { return Name; } set { Name = val; } }
- 我們利用 XmlSerializer 透過(guò) XML 序列化來(lái)序列化物件。
考慮下面的程式碼來(lái)示範(fàn) XmlSerializer 的使用:
代碼:
XmlSerializer Serializer = new XmlSerializer(typeof(Prod)); using (TextWriter Writer = new StreamWriter(@"C:\Prod.xml")) { xmlSerializer.Serialize(Writer, prodObject); }
4.自訂序列化
- 為了控制某種類型實(shí)例的序列化和反序列化,我們使用自訂序列化。
- 可以透過(guò)實(shí)作ISerialized介面來(lái)實(shí)現(xiàn)自訂序列化。
- GetObjectData() 方法由 ISerialized 介面宣告。
考慮下面的程式碼,透過(guò)實(shí)作 ISerialized 介面來(lái)示範(fàn)自訂序列化:
代碼:
[Serializable] public class Prod : ISerializable { public void GetObjectData(SerializationInfo information, StreamingContext cont) { //Usual code } }
範(fàn)例
下面給出的是 C# 序列化的範(fàn)例:
示範(fàn)序列化概念的 C# 程式。
代碼:
using System; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; //a namespace called demo is created namespace Demo { //Serializable attribute is declared [Serializable] //a class check is defined which will be used for serialization class Check { public int identity; public String nam; static void Main(string[] args) { //an object of the check class is created to serialize it to the file Example.txt Check ob = new Check(); ob.identity = 10; ob.nam = "Shobha"; //a file stream is created IFormatter format = new BinaryFormatter(); Stream stream1 = new FileStream(@"E:\Example.txt",FileMode.Create,FileAccess.Write); //serialization of the object of the class check is done format.Serialize(stream1, ob); stream1.Close(); //a file stream is created stream1 = new FileStream(@"E:\Example.txt",FileMode.Open,FileAccess.Read); //the object of the class check is deserialized Check ob1 = (Check)format.Deserialize(stream1); //the data is written to the console Console.WriteLine(ob1.identity); Console.WriteLine(ob1.nam); Console.ReadKey(); } } }
Output:
In the above program, a namespace called demo is defined. Then a Serializable attribute is defined. A class check is defined to demonstrate the concept of serialization using this class. Two properties identity and nam are defined in the class to which the values 10 and Shobha are assigned respectively. Then an object of the check class is created to serialize it to the file Example.txt. Then a formatter class is defined to convert the object of the class check to a binary stream.
Then a file stream object is created to open the file Example.txt in write mode to write the values of the properties identity and nam into it. Then serialize method is used to transfer the binary data into the text file. Finally, We use deserialize method to deserialize the contents of the text file Example.txt and the data is written to the console as shown in the output snapshot above.
以上是C# 序列化的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門(mén)文章

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6
視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版
神級(jí)程式碼編輯軟體(SublimeText3)

熱門(mén)話題

C# 隨機(jī)數(shù)產(chǎn)生器指南。在這裡,我們討論隨機(jī)數(shù)產(chǎn)生器的工作原理、偽隨機(jī)數(shù)和安全數(shù)的概念。

多線程和異步的區(qū)別在於,多線程同時(shí)執(zhí)行多個(gè)線程,而異步在不阻塞當(dāng)前線程的情況下執(zhí)行操作。多線程用於計(jì)算密集型任務(wù),而異步用於用戶交互操作。多線程的優(yōu)勢(shì)是提高計(jì)算性能,異步的優(yōu)勢(shì)是不阻塞 UI 線程。選擇多線程還是異步取決於任務(wù)性質(zhì):計(jì)算密集型任務(wù)使用多線程,與外部資源交互且需要保持 UI 響應(yīng)的任務(wù)使用異步。

C# 質(zhì)數(shù)指南。這裡我們討論c#中素?cái)?shù)的介紹和範(fàn)例以及程式碼實(shí)作。

C#和C 的歷史與演變各有特色,未來(lái)前景也不同。 1.C 由BjarneStroustrup在1983年發(fā)明,旨在將面向?qū)ο缶幊桃隒語(yǔ)言,其演變歷程包括多次標(biāo)準(zhǔn)化,如C 11引入auto關(guān)鍵字和lambda表達(dá)式,C 20引入概念和協(xié)程,未來(lái)將專注於性能和系統(tǒng)級(jí)編程。 2.C#由微軟在2000年發(fā)布,結(jié)合C 和Java的優(yōu)點(diǎn),其演變注重簡(jiǎn)潔性和生產(chǎn)力,如C#2.0引入泛型,C#5.0引入異步編程,未來(lái)將專注於開(kāi)發(fā)者的生產(chǎn)力和雲(yún)計(jì)算。

可以採(cǎi)用多種方法修改 XML 格式:使用文本編輯器(如 Notepad )進(jìn)行手工編輯;使用在線或桌面 XML 格式化工具(如 XMLbeautifier)進(jìn)行自動(dòng)格式化;使用 XML 轉(zhuǎn)換工具(如 XSLT)定義轉(zhuǎn)換規(guī)則;或者使用編程語(yǔ)言(如 Python)進(jìn)行解析和操作。修改時(shí)需謹(jǐn)慎,並備份原始文件。
