大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章给大家分享的是有关C#如何实现序列化和反序列化的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创新互联专注为客户提供全方位的互联网综合服务,包含不限于做网站、成都网站制作、硚口网络推广、小程序定制开发、硚口网络营销、硚口企业策划、硚口品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供硚口建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
.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#如何实现序列化和反序列化”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!