请高手讲讲,串行化有什么用处?
解决方案 »
- 项目中如何使用sqlhelper
- c# winForm 如何将照片解析成XML文件
- 初学者 问 怎么运行别人的程序
- 有人遇到这种情况吗?
- 请问一个控件--》显示html
- 几句唠叨话!发泻一下!
- SOS!!要求解析xml文件,所有节点显示为treeview的节点(包括子节点),,用xmltextreader还是xmldocument??
- 请教:怎样实现链表结构
- 我在C#书上看到private HTTPChannel channe的语句,但是在试的时候就是出错。那位可以帮帮忙!!?
- winform ListView与imageList显示图片后,Checkbox显示问题
- 小问题:怎样获得用户名
- 真的没有人帮我解决这个问题吗?显示时间与禁用关闭按钮 (已经困住我好几天了,急呀)
序列化的目的:
1、以某种存储形式使自定义对象持久化;
2、将对象从一个地方传递到另一个地方。
实质上序列化机制是将类的值转化为一个一般的(即连续的)字节流,然后就可以将该流写到磁盘文件或任何其他流化目标上。而要想实际的写出这个流,就要使用那些实现了IFormatter接口的类里的Serialize和Deserialize方法。
在.net框架里提供了这样两个类:
一、BinaryFormatter
BinaryFormatter使用二进制格式化程序进行序列化。您只需创建一个要使用的流和格式化程序的实例,然后调用格式化程序的 Serialize 方法。流和要序列化的对象实例作为参数提供给此调用。类中的所有成员变量(甚至标记为 private 的变量)都将被序列化。
首先我们创建一个类:
[Serializable]
public class MyObject {
public int n1 = 0;
public int n2 = 0;
public String str = null;
}
Serializable属性用来明确表示该类可以被序列化。同样的,我们可以用NonSerializable属性用来明确表示类不能被序列化。
接着我们创建一个该类的实例,然后序列化,并存到文件里持久:
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "一些字符串";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
而将对象还原到它以前的状态也非常容易。首先,创建格式化程序和流以进行读取,然后让格式化程序对对象进行反序列化。
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open,
FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject) formatter.Deserialize(fromStream);
stream.Close();
// 下面是证明
Console.WriteLine("n1: {0}", obj.n1);
Console.WriteLine("n2: {0}", obj.n2);
Console.WriteLine("str: {0}", obj.str);
二、SoapFormatter
前面我们用BinaryFormatter以二进制格式来序列化。很容易的我们就能把前面的例子改为用SoapFormatter的,这样将以xml格式化,因此能有更好的可移植性。所要做的更改只是将以上代码中的格式化程序换成 SoapFormatter,而 Serialize 和 Deserialize 调用不变。对于上面使用的示例,该格式化程序将生成以下结果。
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP- ENC=http://schemas.xmlsoap.org/soap/encoding/
xmlns:SOAP- ENV=http://schemas.xmlsoap.org/soap/envelope/
SOAP-ENV:encodingStyle=
"http://schemas.microsoft.com/soap/encoding/clr/1.0
http://schemas.xmlsoap.org/soap/encoding/"
xmlns:a1="http://schemas.microsoft.com/clr/assem/ToFile">
2 public class Person
3 {
4 public Person()
5 {
6 }
7
8 public int Age;
9 public int WeightInPounds;
10 }
下面来看一个小例子,首先要添加命名空间using System.Runtime.Serialization.Formatters.Binary;下面的代码将对象Person进行序列化并存储到一个文件中
1 Person me = new Person();
2
3 me.Age = 34;
4 me.WeightInPounds = 200;
5
6 Stream s = File.Open("Me.dat",FileMode.Create);
7
8 BinaryFormatter bf = new BinaryFormatter();
9
10 bf.Serialize(s,me);
11
12 s.Close();
然后再举一个并行化的例子 Stream s = File.Open("Me.dat",FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); object o = bf.Deserialize(s); Person p = o as Person;
if(p != null)
Console.WriteLine("DeSerialized Person aged:{0} whight:{1}",p.Age,p.WeightInPounds); s.Close();
如果需要对部分字段序列化部分不序列化时,我们可以按照如下设置实现 [Serializable]
public class Person
{
public Person()
{
} public int Age;
[NonSerialized]
public int WeightInPounds;
}
简单说就两点:存储和传输。
同意