C#序列化之二

相應c#下的序列化代碼如下所示,程序把序列化後的數據存入了一個指定的文件file.bin裏,分析這個文件數據主要爲了能讓非.Net下的應用程序讀取序列化後數據,這有助於.net與其他語言平臺的交互.
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace WindowsApplication2
{
    [Serializable]
    public class Object5
    {
        public int i1 = 0;
        public int i2 = 0;
        public float f3=0;
        public string str;
    }
 
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        private System.ComponentModel.Container components = null;
 
        public Form1()
        {
            InitializeComponent();     
}
 
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
 
        #region Windows 窗體設計器生成的代碼
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            this.button1.Location = new System.Drawing.Point(72, 72);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(128, 32);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
 
        }
        #endregion
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
        private void button1_Click(object sender, System.EventArgs e)
        {
            Object5 obj = new Object5();
            obj.i1 = 128;
            obj.i2 = 24;
            obj.f3=1.3f;
            obj.str = "Some String";
 
            double   d1=1.3d;
            float    f1=1.3f;
            int      i1=1;
            string   s1="HelloWorld";
 
            System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.Stream stream = new System.IO.FileStream("File.bin", System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
            formatter.Serialize(stream, obj);
            formatter.Serialize(stream,d1);
            formatter.Serialize(stream,f1);
            formatter.Serialize(stream,i1);
            formatter.Serialize(stream,s1);
 
            stream.Close();
            formatter=null;
        }
    }
}
文件內容如下圖所示:

註解:
1 )00 01 00 00 00 FF FF FF FF 01 00 00 00 00 00 00 00 序列化頭,經過實踐分析,這部分基本在每個序列化後的數據都一樣,下面也可以看到與其他的一樣
2 )0C 02 被序列化後對象描述信息的表示符
3 )00 00 00 51(81)長度,(注意是大端的4位整型),說明後面對象描述信息的長度
4 )自定義對象的描述信息,分析時可以忽略,長度爲81個字符(前一項以說明)
5 )05 01自定義對象的表示符 .
6 )00 00 00 1b (27)自定義對象類描述信息的長度
7 )自定義對象的描述信息,主要類的符號表示 (“WindowsApplication2.Object5”)
8 )自定義對象中的成員條目,例如在上面定義的對象中有四項,分別爲int、int、flaot和string
9 )02 長度爲2的int i1成員
10 )對應成員名稱的定義標示 “i1”
11 )同9項
12 )同10項
13 )同 9項
14 )同 10項
15 )同 9項
16 )同10項
17 )說明自定義對象的各個定義項目是值對象還是其他類型,一共4個字節,00爲值對象
 01爲字符對象 在上面定義的對象爲 int i1 int i2 float f1 string str 對應爲 00 00 00 01 。
 
18)說明17)對應的字段的類型 分別爲 int 08 int 08 int 0b
19) 02 00 00 00 固定,說明其他數據的開始
20)對應obj.i1的值 80 00 00 00(128、小端格式)
21)對應obj.i2的值 18 00 00 00 (24、小端格式)
22)對應obj.f1的值 66 66 a6 3f (1.3的浮點格式)
23)06 03 對象string標示符,說明它是string對象
24、25)00 00 00 0b string對象的長度11(大端),值爲“Some String”
26)0b表示一個對象序列的結束。
27)同1)
28)04 01 double類型的表示符號
29)00 00 00 0d(13) 類型長度(大端)
30)對象定義標示符( “System.Double”
31) 01 00 00 00(1)包含一個成員
32)07 (7)成員的定義標示符長度
33) 成員的定義符號 “m_value”
34) 00 說明是值類型  06說明是double類型
35)CD CC CC CC CC CC F4 3F double類型對應的數值
36)0b一個序列對象的結束
 
其他的註釋分析就在這裏不在重複了,原理都一樣的!
經過測試得到的類型與表示符的部分對應關係,如下所示
bool 01
byte 02
uint 0f
char 03
ulong 10
ushort 0e
decimal 05
int     08
sbyte   0a
short   07
double 06
float   0b
long    09
string 06 03 /06 04
 
 
值類型 00
string 01
object 02
[]     07
struct 04
 
這裏分析只是一部份!主要隨着對象的複雜,會涌現出沒有分析到的標示符,如果你發現新的請給我發一封電子郵件,萬分感謝!
這時大家也可以體會出爲什麼xml序列化的存在了,不同系統實現數據想要容易的多了,但xml有一個缺點就是數據量大.這是與二進制序列化所不能比的!
Xml序列化代碼如下:
System.Xml.Serialization.XmlSerializer formatter=new System.Xml.Serialization.XmlSerializer(obj.GetType());
 
對象Object5序列化後的xml文件如下:
<?xml version="1.0"?>
<Object5 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <i1>128</i1>
 <i2>24</i2>
 <f3>1.3</f3>
 <str>Some String</str>
</Object5>
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章