1、使用BinaryFormatter进行串行化
串行化的文件是二进制格式,几乎所有的对象都能顺利串行化,目前还没有发现不能串行化的对象。
- public enum SexType
- {
- Male,
- Female
- }
- [Serializable()]
- public class Item
- {
- private int id;
- public int ID
- {
- get { return id; }
- set { id = value; }
- }
- private string name;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- public Item() { } // 必须有默认构造函数,才能xml序列化
- public Item(int id, string name)
- {
- ID = id;
- Name = name;
- }
- public override string ToString()
- {
- return id.ToString() + "," + name;
- }
- public static Item ToObject(string str)
- {
- Item item = new Item();
- item.id = int.Parse(str.Split(',')[0]);
- item.name = str.Split(',')[1];
- return item;
- }
- }
- [Serializable()]
- public class ItemSub : Item
- {
- private SexType sex;
- public SexType Sex
- {
- get { return sex; }
- set { sex = value; }
- }
- public ItemSub() { } // 必须有默认构造函数,才能xml序列化
- public ItemSub(int id, string name, SexType sex)
- : base(id, name)
- {
- this.sex = sex;
- }
- }
- [Serializable()]
- public class ListBuffer : ISerializable
- {
- private List<string> list = new List<string>();
- public void Add(string str)
- {
- lock (list)
- {
- list.Add(str);
- }
- }
- public void Remove(string str)
- {
- lock (list)
- {
- list.Remove(str);
- }
- }
- public int Count
- {
- get { return list.Count; }
- }
- }
- [Serializable()]
- public class ListBufferSub : ListBuffer
- {
- private List<Item> listItem = new List<Item>();
- public void AddItem(Item item)
- {
- lock (listItem)
- {
- listItem.Add(item);
- }
- }
- public void Remove(Item item)
- {
- }
- }
- [Serializable()]
- public class BinarySerialize
- {
- private int id;
- public int ID
- {
- get { return id; }
- set { id = value; }
- }
- private string name;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- private SexType sex;
- public SexType Sex
- {
- get { return sex; }
- set { sex = value; }
- }
- private List<string> listStr;
- public List<string> ListStr
- {
- get { return listStr; }
- set { listStr = value; }
- }
- private List<Item> listItem;
- public List<Item> ListItem
- {
- get { return listItem; }
- set { listItem = value; }
- }
- private ListBuffer buffer = new ListBuffer();
- public ListBuffer Buffer
- {
- get { return buffer; }
- set { buffer = value; }
- }
- private ListBufferSub bufferSub = new ListBufferSub();
- public ListBufferSub BufferSub
- {
- get { return bufferSub; }
- set { bufferSub = value; }
- }
- private List<ListBuffer> listBuffer;
- public List<ListBuffer> ListBuffer
- {
- get { return listBuffer; }
- set { listBuffer = value; }
- }
- }
- public sealed class ConfigurationManagerBinarySerialize
- {
- private static string path = System.Windows.Forms.Application.StartupPath + "\\BinarySerialize.bat";
- public static BinarySerialize Get()
- {
- if (!File.Exists(path))
- return null;
- BinaryFormatter b = new BinaryFormatter();
- using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
- {
- return (BinarySerialize)b.Deserialize(fs);
- }
- }
- public static void Set(BinarySerialize hr)
- {
- BinaryFormatter b = new BinaryFormatter();
- using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
- {
- b.Serialize(fs, hr);
- }
- }
- }
备注:
不管是多么复杂的类,BinaryFormatter总是能够正确的进行序列化和反序列化操作,而且不需要做任何特殊的处理。所以对于大型的类,需要序列化时,优先选择BinaryFormatter。