序列化

 .net中对象的序列化是指将对象的状态存储起来,先将对象的字段和属性以及类名转换为字节流,然后再把字节流写入数据流。通过对对象反序列化,得到原对象完全相同的副本。

对象的序列化主要的目的是将对象持久化,经过持久化的对象可以从一个地方传输到另一个地方。


在.net中, IFormatter接口提供了对象序列化的功能。他有两个公有的方法:

反序列化对象方法
Deserialize : Deserializes the data on the provided stream and reconstitutes the graph of objects。

序列化对象方法
Serialize:Serializes an object, or graph of objects with the given root to the provided stream。

我们可以将对象序列化成两种格式:

BinaryFormatter :将对象序列化为二进制格式
SoapFormatter:将对象序列化为Soap格式

代码:

//要进行序列化的类
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif
 5 None.gif namespace  SerializeDemos
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif    [Serializable]
 8InBlock.gif    public class Car
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        private string _Price;
11InBlock.gif        private string _Owner;
12InBlock.gif        public string Price
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn this._Price; }
15ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis._Price = value; }
16ExpandedSubBlockEnd.gif        }

17InBlock.gif        public string Owner
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn this._Owner; }
20ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis._Owner = value; }
21ExpandedSubBlockEnd.gif        }

22InBlock.gif        public Car(string o, string p)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            this.Price = p;
25InBlock.gif            this.Owner = o;
26ExpandedSubBlockEnd.gif        }

27ExpandedSubBlockEnd.gif    }

28ExpandedBlockEnd.gif}

29 None.gif

//序列化以及反序列化对象
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.ComponentModel;
 4 None.gif using  System.Data;
 5 None.gif using  System.Drawing;
 6 None.gif using  System.Text;
 7 None.gif using  System.Windows.Forms;
 8 None.gif using  System.IO;
 9 None.gif using  System.Runtime.Serialization.Formatters.Binary;
10 None.gif using  System.Runtime.Serialization.Formatters.Soap;
11 None.gif using  System.Xml.Serialization;
12 None.gif
13 None.gif
14 None.gif namespace  SerializeDemos
15 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
16InBlock.gif
17InBlock.gif    public partial class Form1 : Form
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        public Form1()
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            InitializeComponent();
22ExpandedSubBlockEnd.gif        }

23InBlock.gif        private void Form1_Load(object sender, EventArgs e)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif
26ExpandedSubBlockEnd.gif        }

27InBlock.gif
28InBlock.gif        private void button1_Click(object sender, EventArgs e)
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            Car car = new Car(this.txtOwner.Text, this.txtPrice.Text);
31InBlock.gif            FileStream fileStream = new FileStream("SerializedCar.bin", FileMode.Create);
32InBlock.gif            // 用二进制格式序列化
33InBlock.gif            BinaryFormatter binaryFormatter = new BinaryFormatter();
34InBlock.gif            binaryFormatter.Serialize(fileStream, car);
35InBlock.gif            fileStream.Close();
36InBlock.gif            MessageBox.Show("Successful");
37ExpandedSubBlockEnd.gif        }

38InBlock.gif
39InBlock.gif        private void button2_Click(object sender, EventArgs e)
40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
41InBlock.gif            Car car = new Car(this.txtOwner.Text, this.txtPrice.Text);
42InBlock.gif            FileStream fileStream = new FileStream("SerializedCar.Soap", FileMode.Create);
43InBlock.gif            // 序列化为Soap
44InBlock.gif            SoapFormatter formatter = new SoapFormatter();
45InBlock.gif            formatter.Serialize(fileStream, car);
46InBlock.gif            fileStream.Close();
47InBlock.gif            MessageBox.Show("Successful");
48ExpandedSubBlockEnd.gif        }

49InBlock.gif
50InBlock.gif        private void button3_Click(object sender, EventArgs e)
51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
52InBlock.gif            Car car = new Car(this.txtOwner.Text, this.txtPrice.Text);
53InBlock.gif            FileStream fileStream = new FileStream("SerializedCar.xml", FileMode.Create);
54InBlock.gif            // 序列化为xml
55InBlock.gif            SoapFormatter formatter = new SoapFormatter();
56InBlock.gif            formatter.Serialize(fileStream, car);
57InBlock.gif            fileStream.Close();
58InBlock.gif            MessageBox.Show("Successful");
59ExpandedSubBlockEnd.gif        }

60InBlock.gif
61InBlock.gif        private void button4_Click(object sender, EventArgs e)
62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  
63InBlock.gif            System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
64InBlock.gif            //二进制格式反序列化
65InBlock.gif            Stream stream = new FileStream("SerializedCar.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
66InBlock.gif            Car mycar = (Car)formatter.Deserialize(stream);
67InBlock.gif            stream.Close();
68InBlock.gif            MessageBox.Show(string.Format("Price of Car: {0},\n Owner:{1}", mycar.Price, mycar.Owner));
69ExpandedSubBlockEnd.gif        }

70InBlock.gif
71InBlock.gif        private void button5_Click(object sender, EventArgs e)
72ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
73InBlock.gif            System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
74InBlock.gif            //Soap格式反序列化
75InBlock.gif            Stream stream = new FileStream("SerializedCar.Soap", FileMode.Open, FileAccess.Read, FileShare.Read);
76InBlock.gif            Car mycar = (Car)formatter.Deserialize(stream);
77InBlock.gif            stream.Close();
78InBlock.gif            MessageBox.Show(string.Format("Price of Car: {0},\n Owner:{1}", mycar.Price, mycar.Owner));
79ExpandedSubBlockEnd.gif        }

80InBlock.gif
81InBlock.gif        private void button6_Click(object sender, EventArgs e)
82ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
83InBlock.gif            System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
84InBlock.gif            //Xml格式反序列化
85InBlock.gif            Stream stream = new FileStream("SerializedCar.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
86InBlock.gif            Car mycar = (Car)formatter.Deserialize(stream);
87InBlock.gif            stream.Close();
88InBlock.gif            MessageBox.Show(string.Format("Price of Car: {0},\n Owner:{1}", mycar.Price, mycar.Owner));
89ExpandedSubBlockEnd.gif        }

90ExpandedSubBlockEnd.gif    }

91ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值