.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#序列化和反序列化的例子就和大家讨论到这里。
【编辑推荐】