您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關C#如何實現序列化和反序列化的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
.net下有一種技術叫做對象序列化,說得通俗一點,C#序列化就是把一個對象保存到一個文件或數據庫字段中去,C#反序列化就是在需要的時候再把這個文件轉化成原來的對象使用。
在.NET中常見的C#序列化的方法主要也有三個:二進制序列化、XML序列化、SOAP序列化。
下面通過一個小例子來說明這三種方法的使用。
using System; using System.Collections.Generic; using System.Text; namespace FileSerializer { [Serializable] public class Book { string id; string name; public string Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } public Book() { } public Book(string id,string name) { this.id = id; this.name = name; } public override string ToString() { return "編號:" + id + "\t名稱:" + name; } } } using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml.Serialization; namespace FileSerializer { public abstract class Serializer< T> { string filePath; public string FilePath { get { return filePath; } set { filePath = value; } } public Serializer(string filePath) { this.filePath = filePath; } public void Serialize(T serializeObj) { using (Stream st = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { S(st, serializeObj); } } protected abstract void S(Stream st, T serializeObj); public T Deserialize() { using (Stream st = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { return D(st); } } protected abstract T D(Stream st); } } using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace FileSerializer { class SerializerBinary< T> : Serializer< T> { public SerializerBinary(string filePath) : base(filePath) { } protected override void S(Stream st, T serializeObj) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(st, serializeObj); } protected override T D(Stream st) { BinaryFormatter bf = new BinaryFormatter(); return (T)bf.Deserialize(st); } } } using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Soap; namespace FileSerializer { public class SerializerSoap< T> : Serializer< T> { public SerializerSoap(string filePath) : base(filePath) { } protected override void S(Stream st, T serializeObj) { SoapFormatter sf = new SoapFormatter(); sf.Serialize(st, serializeObj); } protected override T D(Stream st) { SoapFormatter sf = new SoapFormatter(); return (T)sf.Deserialize(st); } } } using System; using System.Collections.Generic; using System.Text; using System.Xml.Serialization; using System.IO; namespace FileSerializer { public class SerializerXml< T> : Serializer< T> { public SerializerXml(string filePath) : base(filePath) { } protected override void S(Stream st, T serializeObj) { XmlSerializer xs = new XmlSerializer(typeof(T)); xs.Serialize(st, serializeObj); } protected override T D(Stream st) { XmlSerializer xs = new XmlSerializer(typeof(T)); return (T)xs.Deserialize(st); } } } using System; using System.Collections.Generic; using System.Text; namespace FileSerializer { class Program { static void Main(string[] args) { Book book = new Book("01","C#程序設計入門01"); Serializer< Book> serializer = new SerializerBinary< Book>("bookBinary"); serializer.Serialize(book); Book newbook = serializer.Deserialize(); Console.WriteLine(newbook.ToString()); book = new Book("02", "C#程序設計入門02"); serializer = new SerializerSoap< Book>("bookSoap.soap"); serializer.Serialize(book); newbook = serializer.Deserialize(); Console.WriteLine(newbook.ToString()); book = new Book("03", "C#程序設計入門03"); serializer = new SerializerXml< Book>("bookXml.xml"); serializer.Serialize(book); newbook = serializer.Deserialize(); Console.WriteLine(newbook.ToString()); Console.ReadLine(); } } }
感謝各位的閱讀!關于“C#如何實現序列化和反序列化”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。