日期:2009-12-31  浏览次数:20529 次

C#对象序列化和反序列化,如下代码示例:

  1. using System; 
  2. using System.Text; 
  3. using System.Collections.Generic; 
  4. using System.IO; 
  5. using System.Runtime.Serialization.Formatters.Binary; 
  6.  
  7. class SerializableOperate 
  8.     private static void ObjectSerializable(object obj, string filePath) 
  9.     { 
  10.         FileStream fs = null
  11.         try 
  12.         { 
  13.             fs = new FileStream(filePath, FileMode.Create); 
  14.             BinaryFormatter bf = new BinaryFormatter(); 
  15.             bf.Serialize(fs, obj); 
  16.         } 
  17.         catch (IOException ex) 
  18.         { 
  19.             Console.WriteLine("序列化是出错!"); 
  20.         } 
  21.         finally 
  22.         { 
  23.             if (fs != null
  24.             { 
  25.                 fs.Close(); 
  26.             } 
  27.         } 
  28.     } 
  29.     private static object ObjectUnSerializable(string filePath) 
  30.    &