<C#>序列化

序列化就是为了简化复杂的数据结构的存储提出来的概念。

序列化也就是把类的对象作为一个整体存入文件,反序列化则是相反过程

#using System;
#using System.IO;
#using System.Collections.Generic;
#using System.Runtime.Serialization.Formatters.Binary;
#using System.Runtime.Serialization;

class SerialFile
{
    static void Main()
   {
             Dictionary<string,string>  h = new Dictionary<string,string>();
             h.Add("Key1","Value1");
             h.Add("Key2","Value2");
             FileStream fs = new FileStream(@"d:/d.dat",FileMode.Create);
             BinaryFormatter formatter = new BinaryFormatter();
             formatter.Serialize(fs,h);
             fs.close();
             fs = new FileStream(@"d:/d.dat",FileMode.Open);
             h.Clear();
             h = (Dictionary<string,string>)formatter.Deserialize(fs);
             fs.Close();
             foreach(KeyValuePair<string,string> h1 in h)
             {
                       Console.WriteLine{"{0}:{1}",h1.Key,h1.Value};
              }
   }  
}    

 

posted @ 2014-07-13 21:22  伍锋  阅读(177)  评论(0编辑  收藏  举报