您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章给大家分享的是有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。