總結:
①序列化基本是指把一個對象保存到文件或流中,比如可以把文件序列化以保存到Xml中,或一個磁盤文件中
②序列化以某種存儲形式使自定義對象持久化;
③將對象從一個地方傳遞到另一個地方。
④將類的值轉化為一個一般的(即連續的)字節流,然后就可以將該流寫到磁盤文件或任何其他流化目標上。
⑥序列是指將對象的實例狀態存儲到存儲媒體的過程。
在此過程中,先將對象的公共字段以及類的名稱(包括類的程序集)轉換為字節流,然后再把字節流寫入數據流。在隨后對對象進行反序列化時,將創建出與原對象完全相同的副本。
⑦用處非常大,用於數據傳輸,對象存貯等。
這些是我通過網上多方參考再結合自己的經驗總結的。還是看實例。
using System;
using System.IO; //文件操作相關
using System.Runtime.Serialization.Formatters.Binary; //包含 BinaryFormatter 類,該類可用於
以二進制格式將對象序列化和反序列化。
namespace SerializeDeserialize
{
class Program
{
static void Main(string[] args)
{
Program P = new Program();
P.SerializeStudent();
P.DeSerializeStudent();
}
public void SerializeStudent()
{
Student c = new Student();
c.Id = 0;
c.Name = "liang";
c.Sex = "女";
c.Qq = "676596050";
c.Homepage = "http://www.zrrj.net";
//創建二進制文件temp.dat
FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
//將Student實例對象序列化給fileStream流:其含義是這時候的Student對象已經存儲到
temp.dat 文件中
b.Serialize(fileStream, c);
fileStream.Flush();
fileStream.Close();
fileStream.Dispose();
}
public void DeSerializeStudent()
{
Student c = new Student();
//下面三個屬性輸出時沒有更改,因為反序列化實例化了一個新的Student
c.Id = 1;
c.Qq = "676596051";
c.Homepage = "http://www.zrrj.net";
FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
BinaryFormatter b = new BinaryFormatter();
//將temp.dat 的文件流反序列化為Student
c = b.Deserialize(fileStream) as Student;
c.Name = "liang";
c.Sex = "男";
Console.Write("編號:" + c.Id + "\n姓名:" + c.Name + "\n性別:" + c.Sex + "\nQQ:"
+ c.Qq + "\n主頁:" + c.Homepage);
Console.ReadLine();
//釋放文件流資源
fileStream.Flush();
fileStream.Close();
fileStream.Dispose();
}
/// <summary>
/// 創建6個可讀可寫的屬性
/// </summary>
[Serializable]
public class Student
{
//編號
private int id;
//姓名
private string name;
//性別
private string sex;
//QQ
private string qq;
//主頁
private string homepage;
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Sex
{
get { return sex; }
set { sex = value; }
}
public string Qq
{
get { return qq; }
set { qq = value; }
}
public string Homepage
{
get { return homepage; }
set { homepage = value; }
}
}
}
}
上面的序列化會將Student類實例存儲到temp.dat 文件中,相反的反序列化就會實現將temp.dat中的數
據反向生成Student對象
方便理解,其中在本實例中C:\temp.dat內容是:
KSerializeDeserialize, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
$SerializeDeserialize.Program+Student id name sex qq 灝忔睙 濂?
676596050
可以看出已經轉換為了二進制編碼。
using System;
using System.Collections; //HashTable 所在的命名空間
using System.IO; //FileStream所在的命名空間
using System.Runtime.Serialization.Formatters.Binary; //序列化反序列化進制轉換空間
namespace Same1
{
class Program
{
static void Main(string[] args)
{
Serialize();
Deserialize();
Console.ReadLine();
}
static void Serialize()
{
//創建一個包含值的HashTable最后將被序列化
Hashtable addresses = new Hashtable();
addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");
//為了將HashTable序列化,需要創建一個File Stream
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
// 利用二進制格式化將 Hashtable序列化至文件流中
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, addresses);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Console.WriteLine("序列化失敗原因是: " + e.Message);
throw;
}
finally
{
fs.Flush();
fs.Close();
fs.Dispose();
}
}
static void Deserialize()
{
// 聲明一個HashTable
Hashtable addresses = null;
// 打開你需要反序列化的文件,並以流的形式輸出
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
//反序列化文件流為HashTable
addresses = (Hashtable)formatter.Deserialize(fs);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Console.WriteLine("反序列化失敗,原因是: " + e.Message);
throw;
}
finally
{
fs.Flush();
fs.Close();
fs.Dispose();
}
//為了驗證反序列化是否成功,將HashTale中的鍵、值對輸出
foreach (DictionaryEntry de in addresses)
{
Console.WriteLine("{0} 的出生地是: {1}.", de.Key, de.Value);
}
}
}
}
DateFile.dat 內容是:
System.Collections.Hashtable
LoadFactor Version Comparer HashCodeProvider HashSize Keys Values
System.Collections.IComparer$System.Collections.IHashCodeProvider 霶8?
Mary Jeff Fred "PO Box 112233, Palo Alto, CA 94301 "123
Main Street, Redmond, WA 98052 987 Pine Road, Phila., PA 19116
