以下文章提供了 C# 序列化的概述。將物件實例轉換為資料流的過程稱為序列化,並且物件實例的狀態轉換為資料流,因為它可以跨不同網路傳輸並持久保存在儲存位置中。這是序列化的一個優點,可以將轉換後的資料流以跨平台相容的格式跨不同網路傳輸,並將轉換後的串流資料以持久或非持久物件狀態保存到儲存媒體中,以便可以在不同的網路上傳輸相同的副本。後來創建的。
以下是C#序列化物件的步驟:
演示 [ Serialized ] 類別的範例類別:
代碼:
[Serializable] public class Check { public int code; public string name; }
考慮下面的範例類別來示範 [ NonSerialized() ] 屬性:
代碼:
[Serializable] public class Check { public int code; public string name; [NonSerialized()] Public double price; }
下面給出了 C# 支持的序列化類型:
考慮下面的程式碼來示範 XmlAttribute 的使用:
代碼:
[XmlAttribute("Name")] public string Name { get { return Name; } set { Name = val; } }
考慮下面的程式碼來示範 XmlSerializer 的使用:
代碼:
XmlSerializer Serializer = new XmlSerializer(typeof(Prod)); using (TextWriter Writer = new StreamWriter(@"C:\Prod.xml")) { xmlSerializer.Serialize(Writer, prodObject); }
考慮下面的程式碼,透過實作 ISerialized 介面來示範自訂序列化:
代碼:
[Serializable] public class Prod : ISerializable { public void GetObjectData(SerializationInfo information, StreamingContext cont) { //Usual code } }
下面給出的是 C# 序列化的範例:
示範序列化概念的 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# 序列化的詳細內容。更多資訊請關注PHP中文網其他相關文章!