深度剖析C#序列化和反序列化

来源:互联网 发布:kali linux有什么用 编辑:程序博客网 时间:2024/05/22 06:26

    C#序列化和反序列化,两者的程序处理方式基本一致,都是基于工厂模式的,所谓C#序列化就是是将对象转换为容易传输的格式的过程,一般情况下转化打流文件,放入内存或者IO文件中。例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象,或者和其它应用程序共享使用。相反的,反序列化根据流重新构造对象。.NET自带的有两种序列化对象的方式,Xml和binary的,XML 序列化不转换方法、索引器、私有字段或只读属性(只读集合除外)。要序列化对象的所有字段和属性(公共的和私有的),请使用 BinaryFormatter,而不要使用 XML 序列化。

    C#序列化和反序列化的实例应用剖析:

    二进制的C#序列化的方式:

    例如我们有个对象:

    1. [Serializable]public class ClassToSerialize{  
    2. public int id=100;  
    3. public string name="Name";  
    4. }  

    需要序列化该对象,必须在给该类加上Serializable的属性,然后创建一个序列化写入的流:FileStream fileStream = new FileStream("temp.dat", FileMode.Create);然后创建二进制格式器:BinaryFormatter b=new BinaryFormatter();然后是序列化:b.Serialize(fileStream,c);,然后关闭保存流。(可以见下面的例子)

    读取一个已经被序列化的对象的时候:操作方式一样,只是

    1. FileStream fileStream = new FileStream(  
    2. "temp.dat", FileMode.Open,   
    3. FileAccess.Read, FileShare.Read);  
    4. ClassToSerialize c =  
    5. (ClassToSerialize)b.Deserialize(fileStream); 

    然后就可以读取了,完整的例子是:

    1. using System;  
    2. using System.IO;  
    3. using System.Runtime.Serialization;  
    4. using System.Runtime.Serialization.Formatters.Binary;  
    5. public class SerialTest{  
    6. public void SerializeNow(){  
    7. ClassToSerialize c=new ClassToSerialize();  
    8. FileStream fileStream = new FileStream(  
    9. "temp.dat", FileMode.Create);  
    10.  
    11. BinaryFormatter b=new BinaryFormatter();  
    12. b.Serialize(fileStream,c);  
    13. fileStream.Close();  
    14. }  
    15. public void DeSerializeNow(){  
    16. ClassToSerialize c=new ClassToSerialize();  
    17. FileStream fileStream = new FileStream(  
    18. "temp.dat", FileMode.Open,  
    19.  FileAccess.Read,  
    20.  FileShare.Read);  
    21. BinaryFormatter b=new BinaryFormatter();  
    22. //SoapFormatter  
    23. c=(ClassToSerialize)b.Deserialize(fileStream);  
    24. Console.WriteLine(c.name);  
    25. fileStream.Close();  
    26. }  
    27. public static void Main(string[] s){  
    28. SerialTest st=new SerialTest();  
    29. st.SerializeNow();  
    30. st.DeSerializeNow();  
    31. }  
    32. }  
    33. [Serializable]  
    34. public class ClassToSerialize{  
    35. public int id=100;  
    36. public string name="Name";  
    37. }  

    这就是自带的序列化和反序列的操作,但是,很多情况下,一个对象比较大,而且很多私有的属性和方法我们不需要,例如在原型模式里面序列化的话,只需要序列Clone方法和一些属性,私有的方法无需要,还例如在读取大规模的IO的时候,读取操作完全不需要... 这时候就需要自己集成重写序列的ISerializable接口:

    实现该接口需要两个注意的,一个就是构造函数,主要是为了反序列,另一个就是GetObjectData,主要是执行序列化,例如我们现在有一个Employee类需要序列化

    1. [Serializable()]  
    2. //Set this attribute to all the classes that want to serialize  
    3. public class Employee : ISerializable   
    4. //derive your class from ISerializable {  
    5. public int EmpId;  
    6. public string EmpName;  
    7. [NonSerialized()]  
    8. public string NoSerialString="NoSerialString-Test";  
    9.  

    需要注意的是我这里的NoSerialString属性前面有[NonSerialized()],就是说默认并不序列化这个属性,而是使用默认值 。

    首先是构造函数:

    1. public Employee(SerializationInfo info, StreamingContext ctxt)  
    2. {  
    3. EmpId = (int)info.GetValue(  
    4. "EmployeeId"typeof(int));  
    5. EmpName = (String)info.GetValue(  
    6. "EmployeeName"typeof(string));  
    7. //NoSerialString =   
    8. //(String)info.GetValue("NoSerialString", typeof(string));  

    然后是C#序列化方法,就是当写入流的时候怎么保存的:

    1. public void GetObjectData(SerializationInfo info, StreamingContext ctxt)  
    2. {  
    3. //You can use any custom name for your name-value pair.  
    4. // But make sure you  
    5. // read the values with the same name.  
    6. //For ex:- If you write EmpId as "EmployeeId"  
    7. // then you should read the same with "EmployeeId"  
    8. info.AddValue("EmployeeId", EmpId);  
    9. info.AddValue("EmployeeName", EmpName);  

    把上面两个方法写入到Employee类,然后写个测试的程序:

    1. public class ObjSerial{  
    2. public static void Main(String[] args){  
    3. Employee mp = new Employee();  
    4. mp.EmpId = 10;  
    5. mp.EmpName = "Omkumar";  
    6. mp.NoSerialString = "你好啊";  
    7.  
    8.    //C#序列化和反序列化之序列化  
    9. Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);  
    10. BinaryFormatter bformatter = new BinaryFormatter();  
    11.  
    12. Console.WriteLine("Writing Employee Information");  
    13. bformatter.Serialize(stream, mp);  
    14. stream.Close();  
    15.  
    16.  
    17. mp = null;  
    18.    //C#序列化和反序列化之反序列  
    19. stream = File.Open("EmployeeInfo.osl", FileMode.Open);  
    20. bformatter = new BinaryFormatter();  
    21.  
    22. Console.WriteLine("Reading Employee Information");  
    23. mp = (Employee)bformatter.Deserialize(stream);  
    24. stream.Close();  
    25.  
    26. Console.WriteLine(  
    27. "Employee Id: {0}",mp.EmpId.ToString());  
    28. Console.WriteLine(  
    29. "Employee Name: {0}",mp.EmpName);  
    30. Console.WriteLine(  
    31. "Employee NoSerialString: {0}",mp.NoSerialString);  
    32.  
    33. }  
    34. }  

    C#序列化和反序列化程序执行的结果是:

    1. Writing Employee Information  
    2. Reading Employee Information  
    3. Employee Id: 10  
    4. Employee Name: Omkumar  
    5. Employee NoSerialString: NoSerialString-Test 

    看到Employee NoSerialString:属性的值没有,它保持默认值,没有序列化。

    C#序列化和反序列化的理解就向你介绍到这里,希望对你了解和学习更重要的是对你使用C#序列化和反序列化有所帮助。

原创粉丝点击