.net c# 序列化和反序列
?所謂的序列化就是是將對(duì)象轉(zhuǎn)換為容易傳輸?shù)母袷降倪^(guò)程,一般情況下轉(zhuǎn)化打流文件,放入內(nèi)存或者IO文件中。例如,可以序列化一個(gè)對(duì)象,然后使用 HTTP 通過(guò) Internet 在客戶端和服務(wù)器之間傳輸該對(duì)象,或者和其它應(yīng)用程序共享使用。反之,反序列化根據(jù)流重新構(gòu)造對(duì)象。
??
? .NET自帶的有兩種序列化對(duì)象的方式,Xml和binary的,XML 序列化不轉(zhuǎn)換方法、索引器、私有字段或只讀屬性(只讀集合除外)。要序列化對(duì)象的所有字段和屬性(公共的和私有的),請(qǐng)使用 BinaryFormatter,而不要使用 XML 序列化(參見(jiàn)ms-help://MS.NETFramework.v20.chs/dv_fxserialization/html/8c63200d-db63-4a03-a93d-21641623df62.htmXML 和 SOAP 序列化)。
?? 兩者的程序處理方式基本一致,都是基于工廠模式的,下面我就只說(shuō)二進(jìn)制的序列化的方式:
?? 例如我們有個(gè)對(duì)象: [Serializable]public class ClassToSerialize{
??? public int id=100;
??? public string name="Name";
}
??
需要序列化該對(duì)象,必須在給該類加上Serializable的屬性,然后創(chuàng)建一個(gè)序列化寫(xiě)入的流:FileStream fileStream = new FileStream("temp.dat", FileMode.Create);然后創(chuàng)建二進(jìn)制格式器:BinaryFormatter b=new BinaryFormatter();然后是序列化:b.Serialize(fileStream,c);,然后關(guān)閉保存流。(可以見(jiàn)下面的例子)
?
?? 讀取一個(gè)已經(jīng)被序列化的對(duì)象的時(shí)候:操作方式一樣,只是FileStream fileStream = new FileStream("temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
ClassToSerialize c =(ClassToSerialize)b.Deserialize(fileStream);
然后就可以讀取了,完整的例子是:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class SerialTest{
??? public void SerializeNow(){
??????? ClassToSerialize c=new ClassToSerialize();
??????? FileStream fileStream = new FileStream("temp.dat", FileMode.Create);
??????? BinaryFormatter b=new BinaryFormatter();
??????? b.Serialize(fileStream,c);
??????? fileStream.Close();
??? }
??? public void DeSerializeNow(){
??????? ClassToSerialize c=new ClassToSerialize();
??????? FileStream fileStream = new FileStream("temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
??????? BinaryFormatter b=new BinaryFormatter();//SoapFormatter
??????? c=(ClassToSerialize)b.Deserialize(fileStream);
??????? Console.WriteLine(c.name);
??????? fileStream.Close();
??? }
??? public static void Main(string[] s){
??????? SerialTest st=new SerialTest();
??????? st.SerializeNow();
??????? st.DeSerializeNow();
??? }
}
[Serializable]
public class ClassToSerialize{
??? public int id=100;
??? public string name="Name";
}
這就是自帶的序列化和反序列的操作,但是,很多情況下,一個(gè)對(duì)象比較大,而且很多私有的屬性和方法我們不需要,例如在原型模式里面序列化的話,只需要序列Clone方法和一些屬性,私有的方法無(wú)需要,還例如在讀取大規(guī)模的IO的時(shí)候,讀取操作完全不需要... 這時(shí)候就需要自己集成重寫(xiě)序列的ISerializable接口:
?
?實(shí)現(xiàn)該接口需要兩個(gè)注意的,一個(gè)就是構(gòu)造函數(shù),主要是為了反序列,另一個(gè)就是GetObjectData,主要是執(zhí)行序列化,例如我們現(xiàn)在有一個(gè)Employee類需要序列化??? [Serializable()]??? //Set this attribute to all the classes that want to serialize
??? public class Employee : ISerializable //derive your class from ISerializable {
??????? public int EmpId;
??????? public string EmpName;
??????? [NonSerialized()]
??? public string NoSerialString="NoSerialString-Test";
}
,需要注意的是我這里的NoSerialString屬性前面有[NonSerialized()],就是說(shuō)默認(rèn)并不序列化這個(gè)屬性,而是使用默認(rèn)值 。
?首先是構(gòu)造函數(shù):??????? public Employee(SerializationInfo info, StreamingContext ctxt)
??????? {
??????????? EmpId = (int)info.GetValue("EmployeeId", typeof(int));
??????????? EmpName = (String)info.GetValue("EmployeeName", typeof(string));
??????????? //NoSerialString = (String)info.GetValue("NoSerialString", typeof(string));
??????? }
然后是序列化方法,就是當(dāng)寫(xiě)入流的時(shí)候怎么保存的:
??????? public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
??????? {
??????????? //You can use any custom name for your name-value pair. But make sure you
??????????? // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
??????????? // then you should read the same with "EmployeeId"
??????????? info.AddValue("EmployeeId", EmpId);
??????????? info.AddValue("EmployeeName", EmpName);
??????? }
把上面兩個(gè)方法寫(xiě)入到Employee類,然后寫(xiě)個(gè)測(cè)試的程序:
public class ObjSerial{
??? public static void Main(String[] args){
??????? Employee mp = new Employee();
??????? mp.EmpId = 10;
??????? mp.EmpName = "Omkumar";
??????? mp.NoSerialString = "你好啊";
???????????????
?????? //序列化
??????? Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
??????? BinaryFormatter bformatter = new BinaryFormatter();
???????????????
??????? Console.WriteLine("Writing Employee Information");
??????? bformatter.Serialize(stream, mp);
??????? stream.Close();
??????? mp = null;
?????? //反序列
??????? stream = File.Open("EmployeeInfo.osl", FileMode.Open);
??????? bformatter = new BinaryFormatter();
???????????
??????? Console.WriteLine("Reading Employee Information");
??????? mp = (Employee)bformatter.Deserialize(stream);
??????? stream.Close();
???????????????
??????? Console.WriteLine("Employee Id: {0}",mp.EmpId.ToString());
??????? Console.WriteLine("Employee Name: {0}",mp.EmpName);
??????? Console.WriteLine("Employee NoSerialString: {0}",mp.NoSerialString);
??? }
}
執(zhí)行的結(jié)果是:Writing Employee Information
Reading Employee Information
Employee Id: 10
Employee Name: Omkumar
Employee NoSerialString: NoSerialString-Test
?
?看到Employee NoSerialString:屬性的值沒(méi)有,它保持默認(rèn)值,沒(méi)有序列化。
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/Sea_Shore/archive/2009/06/26/4301604.aspx
轉(zhuǎn)載于:https://www.cnblogs.com/fsyiyun/archive/2009/11/18/3946989.html
總結(jié)
以上是生活随笔為你收集整理的.net c# 序列化和反序列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何:在OpenText Workflo
- 下一篇: Visual Studio 2010 U