C# 串行化与反串行化--使用BinaryFormatter进行串行化

1、使用BinaryFormatter进行串行化

串行化的文件是二进制格式,几乎所有的对象都能顺利串行化,目前还没有发现不能串行化的对象。

[csharp]  view plain  copy
  1. public enum SexType  
  2.     {  
  3.         Male,  
  4.         Female  
  5.     }  
  6.     [Serializable()]  
  7.     public class Item  
  8.     {  
  9.         private int id;  
  10.         public int ID  
  11.         {  
  12.             get { return id; }  
  13.             set { id = value; }  
  14.         }  
  15.   
  16.         private string name;  
  17.         public string Name  
  18.         {  
  19.             get { return name; }  
  20.             set { name = value; }  
  21.         }  
  22.   
  23.         public Item() { } // 必须有默认构造函数,才能xml序列化  
  24.         public Item(int id, string name)  
  25.         {  
  26.             ID = id;  
  27.             Name = name;  
  28.         }  
  29.   
  30.         public override string ToString()  
  31.         {  
  32.             return id.ToString() + "," + name;  
  33.         }  
  34.   
  35.         public static Item ToObject(string str)  
  36.         {  
  37.             Item item = new Item();  
  38.             item.id = int.Parse(str.Split(',')[0]);  
  39.             item.name = str.Split(',')[1];  
  40.             return item;  
  41.         }  
  42.     }  
  43.     [Serializable()]  
  44.     public class ItemSub : Item  
  45.     {  
  46.         private SexType sex;  
  47.         public SexType Sex  
  48.         {  
  49.             get { return sex; }  
  50.             set { sex = value; }  
  51.         }  
  52.   
  53.         public ItemSub() { } // 必须有默认构造函数,才能xml序列化  
  54.         public ItemSub(int id, string name, SexType sex)  
  55.             : base(id, name)  
  56.         {  
  57.             this.sex = sex;  
  58.         }  
  59.     }  
  60.   
  61.     [Serializable()]  
  62.     public class ListBuffer : ISerializable  
  63.     {  
  64.         private List<string> list = new List<string>();  
  65.   
  66.         public void Add(string str)  
  67.         {  
  68.             lock (list)  
  69.             {  
  70.                 list.Add(str);  
  71.             }  
  72.         }  
  73.   
  74.         public void Remove(string str)  
  75.         {  
  76.             lock (list)  
  77.             {  
  78.                 list.Remove(str);  
  79.             }  
  80.         }  
  81.   
  82.         public int Count  
  83.         {  
  84.             get { return list.Count; }  
  85.         }  
  86.     }  
  87.     [Serializable()]  
  88.     public class ListBufferSub : ListBuffer  
  89.     {  
  90.         private List<Item> listItem = new List<Item>();  
  91.   
  92.         public void AddItem(Item item)  
  93.         {  
  94.             lock (listItem)  
  95.             {  
  96.                 listItem.Add(item);  
  97.             }  
  98.         }  
  99.   
  100.         public void Remove(Item item)  
  101.         {  
  102.         }  
  103.     }  
  104.   
  105.     [Serializable()]  
  106.     public class BinarySerialize  
  107.     {  
  108.         private int id;  
  109.         public int ID  
  110.         {  
  111.             get { return id; }  
  112.             set { id = value; }  
  113.         }  
  114.   
  115.         private string name;  
  116.         public string Name  
  117.         {  
  118.             get { return name; }  
  119.             set { name = value; }  
  120.         }  
  121.   
  122.         private SexType sex;  
  123.         public SexType Sex  
  124.         {  
  125.             get { return sex; }  
  126.             set { sex = value; }  
  127.         }  
  128.   
  129.         private List<string> listStr;  
  130.         public List<string> ListStr  
  131.         {  
  132.             get { return listStr; }  
  133.             set { listStr = value; }  
  134.         }  
  135.   
  136.         private List<Item> listItem;  
  137.         public List<Item> ListItem  
  138.         {  
  139.             get { return listItem; }  
  140.             set { listItem = value; }  
  141.         }  
  142.   
  143.         private ListBuffer buffer = new ListBuffer();  
  144.         public ListBuffer Buffer  
  145.         {  
  146.             get { return buffer; }  
  147.             set { buffer = value; }  
  148.         }  
  149.   
  150.         private ListBufferSub bufferSub = new ListBufferSub();  
  151.         public ListBufferSub BufferSub  
  152.         {  
  153.             get { return bufferSub; }  
  154.             set { bufferSub = value; }  
  155.         }  
  156.   
  157.         private List<ListBuffer> listBuffer;  
  158.         public List<ListBuffer> ListBuffer  
  159.         {  
  160.             get { return listBuffer; }  
  161.             set { listBuffer = value; }  
  162.         }  
  163.     }  
  164.   
  165.     public sealed class ConfigurationManagerBinarySerialize  
  166.     {  
  167.         private static string path = System.Windows.Forms.Application.StartupPath + "\\BinarySerialize.bat";  
  168.   
  169.         public static BinarySerialize Get()  
  170.         {  
  171.             if (!File.Exists(path))  
  172.                 return null;  
  173.   
  174.             BinaryFormatter b = new BinaryFormatter();  
  175.             using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))  
  176.             {  
  177.                 return (BinarySerialize)b.Deserialize(fs);  
  178.             }  
  179.         }  
  180.   
  181.         public static void Set(BinarySerialize hr)  
  182.         {  
  183.             BinaryFormatter b = new BinaryFormatter();  
  184.             using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))  
  185.             {  
  186.                 b.Serialize(fs, hr);  
  187.             }  
  188.         }  
  189.     }  

备注:

不管是多么复杂的类,BinaryFormatter总是能够正确的进行序列化和反序列化操作,而且不需要做任何特殊的处理。所以对于大型的类,需要序列化时,优先选择BinaryFormatter。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值