C# 串行化与反串行化--自定义序列化

来源:互联网 发布:模具加工中心编程 编辑:程序博客网 时间:2024/06/05 07:32

5、自定义序列化
如果你希望让用户对类进行串行化,但是对数据流的组织方式不完全满意,那么可以通过在自定义类中实现接口来自定义串行化行为。这个接口只有一个方法,GetObjectData.这个方法用于将对类对象进行串行化所需要的数据填进SerializationInfo对象。你使用的格式化器将构造SerializationInfo对象,然后在串行化时调用GetObjectData.如果类的父类也实现了ISerializable,那么应该调用GetObjectData的父类实现。
 
  如果你实现了ISerializable,那么还必须提供一个具有特定原型的构造器,这个构造器的参数列表必须与GetObjectData相同。这个构造器应该被声明为私有的或受保护的,以防止粗心的开发人员直接使用它。

[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.         /// <summary>  
  88.         /// 自定义序列化方法  
  89.         /// 为了让子类能定义自定义序列化方法,这里必须标记为virtual  
  90.         /// 从接口中继承而来的方法没有任何修饰符,必须继承后自己添加  
  91.         /// 如果这个方法不标记为virtual,那么子类将不能重写自己的序列化方法  
  92.         /// </summary>  
  93.         /// <param name="info"></param>  
  94.         /// <param name="ctxt"></param>  
  95.         public virtual void GetObjectData(SerializationInfo info, StreamingContext ctxt)  
  96.         {  
  97.             int index = 0;  
  98.             foreach (string str in list)  
  99.             {  
  100.                 // 由于是列表,所以不能给每个对象分配一个固定的键名,所以这里用索引序号代替  
  101.                 info.AddValue(index.ToString(), str);  
  102.                 index++;  
  103.             }  
  104.         }  
  105.   
  106.         public ListBuffer() { }  
  107.         protected ListBuffer(SerializationInfo info, StreamingContext ctxt)  
  108.         {  
  109.             int i = 0;  
  110.             SerializationInfoEnumerator irator = info.GetEnumerator();  
  111.             while (irator.MoveNext())  
  112.             {  
  113.                 if (irator.Name.IndexOf("sub") == -1) // 这里先要过滤掉子类中添加的键名,否则会异常  
  114.                 {  
  115.                     // 反序列化,从流中找到可用的键  
  116.                     object o = info.GetValue(i.ToString(), typeof(string));  
  117.                     if (o != null)  
  118.                         list.Add(o.ToString());  
  119.                 }  
  120.   
  121.                 i++;  
  122.             }  
  123.         }  
  124.     }  
  125.     [Serializable()]  
  126.     public class ListBufferSub : ListBuffer  
  127.     {  
  128.         private List<Item> listItem = new List<Item>();  
  129.   
  130.         public void AddItem(Item item)  
  131.         {  
  132.             lock (listItem)  
  133.             {  
  134.                 listItem.Add(item);  
  135.             }  
  136.         }  
  137.   
  138.         public void Remove(Item item)  
  139.         {  
  140.         }  
  141.   
  142.         /// <summary>  
  143.         /// 子类要增加自己的序列化对象,必须重载基类的GetObjectData  
  144.         /// 再次重申一遍,基类从接口继承的GetObjectData默认不是virtual的,必须手动加上virtual修饰符  
  145.         /// </summary>  
  146.         /// <param name="info"></param>  
  147.         /// <param name="ctxt"></param>  
  148.         public override void GetObjectData(SerializationInfo info, StreamingContext ctxt)  
  149.         {  
  150.             base.GetObjectData(info, ctxt); // 先调用基类的自定义序列化方法  
  151.   
  152.             int index = base.Count; // 索引序号从基类的索引序号开始  
  153.             foreach (Item item in listItem) // 然后再将子类自己的序列化成员添加到序列流中  
  154.             {  
  155.                 info.AddValue("sub"+index.ToString(), item);  
  156.                 index++;  
  157.             }  
  158.         }  
  159.   
  160.         public ListBufferSub() { }  
  161.         protected ListBufferSub(SerializationInfo info, StreamingContext ctxt)  
  162.             : base(info, ctxt) // 先调用基类的反序列化构造方法(序列化流中包含基类和子类的键,在使用是要注意剔除)  
  163.         {  
  164.             int i = 0;  
  165.             SerializationInfoEnumerator irator = info.GetEnumerator();  
  166.             while (irator.MoveNext())  
  167.             {  
  168.                 if (irator.Name.IndexOf("sub") != -1) // 找到子类的键  
  169.                 {  
  170.                     object o = info.GetValue("sub" + i.ToString(), typeof(Item)); // 获得子类的对象  
  171.                     if (o != null && o is Item)  
  172.                         listItem.Add(o as Item);  
  173.                 }  
  174.                 i++;  
  175.             }  
  176.         }  
  177.     }  
  178.   
  179.     [Serializable()]  
  180.     public class BinarySerialize  
  181.     {  
  182.         private int id;  
  183.         public int ID  
  184.         {  
  185.             get { return id; }  
  186.             set { id = value; }  
  187.         }  
  188.   
  189.         private string name;  
  190.         public string Name  
  191.         {  
  192.             get { return name; }  
  193.             set { name = value; }  
  194.         }  
  195.   
  196.         private SexType sex;  
  197.         public SexType Sex  
  198.         {  
  199.             get { return sex; }  
  200.             set { sex = value; }  
  201.         }  
  202.   
  203.         private List<string> listStr;  
  204.         public List<string> ListStr  
  205.         {  
  206.             get { return listStr; }  
  207.             set { listStr = value; }  
  208.         }  
  209.   
  210.         private List<Item> listItem;  
  211.         public List<Item> ListItem  
  212.         {  
  213.             get { return listItem; }  
  214.             set { listItem = value; }  
  215.         }  
  216.   
  217.         private ListBuffer buffer = new ListBuffer();  
  218.         public ListBuffer Buffer  
  219.         {  
  220.             get { return buffer; }  
  221.             set { buffer = value; }  
  222.         }  
  223.   
  224.         private ListBufferSub bufferSub = new ListBufferSub();  
  225.         public ListBufferSub BufferSub  
  226.         {  
  227.             get { return bufferSub; }  
  228.             set { bufferSub = value; }  
  229.         }  
  230.   
  231.         private List<ListBuffer> listBuffer;  
  232.         public List<ListBuffer> ListBuffer  
  233.         {  
  234.             get { return listBuffer; }  
  235.             set { listBuffer = value; }  
  236.         }  
  237.     }  
  238.   
  239.     public sealed class ConfigurationManagerBinarySerialize  
  240.     {  
  241.         private static string path = System.Windows.Forms.Application.StartupPath + "\\BinarySerialize.bat";  
  242.   
  243.         public static BinarySerialize Get()  
  244.         {  
  245.             if (!File.Exists(path))  
  246.                 return null;  
  247.   
  248.             BinaryFormatter b = new BinaryFormatter();  
  249.             using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))  
  250.             {  
  251.                 return (BinarySerialize)b.Deserialize(fs);  
  252.             }  
  253.         }  
  254.   
  255.         public static void Set(BinarySerialize hr)  
  256.         {  
  257.             BinaryFormatter b = new BinaryFormatter();  
  258.             using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))  
  259.             {  
  260.                 b.Serialize(fs, hr);  
  261.             }  
  262.         }  
  263.     }  

备注:

这里写了一个很复杂的自定义序列化的例子,其中包含集合,继承等关系。

对于需要自定义序列化的类,需要使其继承ISerializable接口,实现GetObjectData函数以及添加一个带相同参数的构造函数。

序列化时,系统调用GetObjectData保存信息,反序列化时,系统调用带参数的构造函数创建对象。

如果基类自定义了序列化,那么子类在自定义序列化方法时首先要调用基类的自定义方法,然后再进行自己的序列化操作。

基类的GetObjectData方法必须注明是virtual的,否则子类无法正常序列化。

自定义序列化必须使用BinaryFormatter进行序列化操作,xml无法正常工作。