我最近學習remoting和web服務時,總是看到一個重要的字眼"序列化".
那什么是序列化呢?
以前我也模模糊糊.
?
為了搞清楚,請和我一起來序列化學習之旅吧.
?
讓我們先看看序列化的定義,以下是微軟的說明:
序列化可被定義為將對象的狀態存儲到存儲媒介中的過程。在此過程中,對象的公共字段和私有字段以及類的名稱(包括包含該類的程序集)都被轉換為字節流,然后寫入數據流。在以后反序列化該對象時,創建原始對象的精確復本
?
序列化一般用在2種地方:
1.將數據保持到存儲中
例如:我知道在Asp.Net Forums中有.Net中序列化和反序列化的應用
在Forums中,有些內容是不固定的,如用戶資料,除了一些基本資料,可能還要MSN、個人主頁、簽名等.我們一般是一個屬性對應于表中的一個字段,要是以后我們增加一些新屬性,就得增加表字段,還要修改存儲過程,這樣其不麻煩?
在Asp.Net Forums中把用戶資料序列化為2進制,這樣用一個表字段就可以解決問題,并且擴展性好。
?
2.通過值將對象從一個應用程序域發送到另一個應用程序域中
remoting和web服務就是典型的應用
?
說多了沒用,讓我們來一段代碼吧
先定義一個類
?1
using
?System;
?2
?3
namespace
?SerializTest
?4
{
?5
????[Serializable]
?6
????
public
?
class
?Class2
?7
????
{
?8
????????
private
?
string
?name;
?9
????????[NonSerialized]
10
????????
private
?
int
?account;
11
????????
12
????????
public
?Class2(
string
?name,
int
?account)
13
????????
{
14
????????????
this
.account
=
account;
15
????????????
this
.name
=
name;
16
????????}
17
18
????????
public
?
int
?Account
19
????????
{
20
????????????
get
21
????????????
{
22
????????????????
return
?account;
23
????????????}
24
????????}
25
26
????????
public
?
string
?Name
27
????????
{
28
????????????
get
29
????????????
{
30
????????????????
return
?name;
31
????????????}
32
????????}
33
????}
34
}
35

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

序列化一個類的最簡單的方式是使用Serializable屬性
當然還可以通過在對象上實現ISerializable接口,自定義序列化
標記了Serializable屬性的類,類里面的所有成員都將被序列化,私有的變量也在內
當然我們也可以有選擇的序列化類里面的字段
例如類里面的一些敏感數據,我們可以不對其進行序列化
通過用NonSerialized屬性標記成員變量,可以防止它們被序列化
NonSerialized屬性只可以用在類的某個字段上
好了,再來一段俺喜歡的控制臺來看看到底是怎么回事
?1
using
?System;
?2
using
?System.IO;
?3
using
?System.Runtime.Serialization;
?4
using
?System.Runtime.Serialization.Formatters.Binary;
?5
using
?System.Runtime.Serialization.Formatters.Soap;
?6
?7
namespace
?SerializTest
?8
{
?9
????
class
?Class1
10
????
{
11
????????[STAThread]
12
????????
static
?
void
?Main(
string
[]?args)
13
????????
{
14
????????????
string
?fileName
=
"
MyFile.dat
"
;
15
????????????Class2?my
=
new
?Class2(
"
Serializ?TestSerializ
"
,
987
);?
16
????????????Console.WriteLine(
"
初始化Class2類的一個實例my,my的賬號=987,my的名字=Serializ?TestSerializ
"
);
17
18
????????????
//
序列化過程開始,我們把Class2的實例二進制序列化到文件MyFile.dat中
19
????????????IFormatter?formatter1
=
new
??BinaryFormatter();
20
????????????Stream?stream1?
=
?
new
?FileStream(fileName,?FileMode.Create,?FileAccess.Write,FileShare.None);
21
????????????formatter1.Serialize(stream1,my);
22
????????????stream1?.Close();
23
24
????????????
//
反序列化過程開始,我們把Class2的實例從文件MyFile.dat中取出來
25
????????????IFormatter?formatter?
=
?
new
?BinaryFormatter();
26
????????????Stream?stream?
=
?
new
?FileStream(fileName,?FileMode.Open,?FileAccess.Read,?FileShare.Read);
27
????????????Class2?c2?
=
?(Class2)?formatter.Deserialize(stream);
28
????????????stream.Close();
29
30
????????????Console.WriteLine(
"
c2的名字=
"
+
?c2.Name?);
31
????????????Console.WriteLine(
"
c2的賬號=
"
+
?c2.Account.ToString()?);
32
33
????????????Console.ReadLine();
34
????????}
35
????}
36
}

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36


我們可以看到由于類Class2的account字段運用了NonSerialized屬性
反序列化后我們是看不到Class2類實例的賬號,只能看到名字
讓我們再看一段,同時序列化多個對象的代碼
?1
using
?System;
?2
using
?System.IO;
?3
using
?System.Runtime.Serialization;
?4
using
?System.Runtime.Serialization.Formatters.Binary;
?5
using
?System.Runtime.Serialization.Formatters.Soap;
?6
?7
namespace
?SerializTest
?8
{
?9
????
class
?Class1
10
????
{
11
????????[STAThread]
12
????????
static
?
void
?Main(
string
[]?args)
13
????????
{
14
????????????
string
?fileName
=
"
MyFile.dat
"
;
15
????????????Class2[]?myClass
=
{
new
?Class2(
"
a
"
,
123
),
new
?Class2(
"
b
"
,
456
),
new
?Class2(
"
c
"
,
789
)}
;
16
17
????????????
//
序列化過程開始
18
????????????
//
我們把類的幾個實例序列化到一個文件中,這樣比起分別序列化可以節約空間
19
????????????IFormatter?formatter
=
new
?BinaryFormatter();
20
????????????Stream?stream?
=
?
new
?FileStream(fileName,?FileMode.Create,?FileAccess.Write,FileShare.None);
21
????????????
for
(
int
?i
=
0
;i
<
myClass.Length;i
++
)
22
????????????
{
23
????????????????formatter.Serialize(stream,myClass[i]);
24
????????????}
25
????????????stream.Close();
26
27
????????????
//
反序列化過程開始
28
????????????IFormatter?formatter11
=
new
??BinaryFormatter();
29
????????????Stream?fs?
=
?
new
?FileStream(fileName,?FileMode.Open,?FileAccess.Read,FileShare.Read);
30
????????????
long
?ii
=
?fs.Length;
31
????????????System.Collections.ArrayList?list
=
new
?System.Collections.ArrayList();
32
????????????
//
從文件中逐個讀出Class2的實例并反序列化
33
????????????
while
?(fs.Position
!=
fs.Length)
34
????????????
{
35
????????????????list.Add(formatter11.Deserialize(fs));
36
????????????}
37
????????????fs.Close();
38
????????????Console.WriteLine(
"
反序列化結果:
"
);
39
????????????
foreach
(
object
?o?
in
?list)
40
????????????
{
41
????????????????Class2?class2
=
o?
as
?Class2;
42
????????????????
if
?(class2
!=
null
)
43
????????????????
{
44
????????????????????Console.WriteLine(
"
名字=
"
+
?class2.Name?);
45
????????????????????Console.WriteLine(
"
賬號=
"
+
?class2.Account.ToString());
46
????????????????}
47
????????????}
48
49
????????????Console.ReadLine();
50
????????}
51
????}
52
}

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52


其實數組也是可以序列化的類
那么我們可以把上面的代碼做些簡化
?1
using
?System;
?2
using
?System.IO;
?3
using
?System.Runtime.Serialization;
?4
using
?System.Runtime.Serialization.Formatters.Binary;
?5
using
?System.Runtime.Serialization.Formatters.Soap;
?6
?7
namespace
?SerializTest
?8
{
?9
????
class
?Class1
10
????
{
11
????????[STAThread]
12
????????
static
?
void
?Main(
string
[]?args)
13
????????
{
14
???????????

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
