对象的序列化
.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格式
代码:
//要进行序列化的类
//序列化以及反序列化对象
对象的序列化主要的目的是将对象持久化,经过持久化的对象可以从一个地方传输到另一个地方。
在.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
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace SerializeDemos
6
2
3
4
5
6
//序列化以及反序列化对象
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.IO;
9
using System.Runtime.Serialization.Formatters.Binary;
10
using System.Runtime.Serialization.Formatters.Soap;
11
using System.Xml.Serialization;
12
13
14
namespace SerializeDemos
15
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15