C# Serializable
Posted 笨鹤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# Serializable相关的知识,希望对你有一定的参考价值。
System.SerializableAttribute
串行化是指存储和获取磁盘文件、内存或其他地方中的对象。在串行化时,所有的实例数据都保存到存储介质上,在取消串行化时,对象会被还原,且不能与其原实例区别开来。
只需给类添加Serializable属性,就可以实现串行化实例的成员。
并行化是串行化的逆过程,数据从存储介质中读取出来,并赋给类的实例变量。
例:
[Serializable]
2
public class Person
3
{
4
public Person()
5
{
6
}
7![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
8
public int Age;
9
public int WeightInPounds;
10
}
2
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
3
![](https://image.cha138.com/20210612/b949d1d7559049ea9645423338197bdf.jpg)
4
![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
5
![](https://image.cha138.com/20210612/f7a5a3cb56f44707840854969a7ce831.jpg)
6
![](https://image.cha138.com/20210612/7fb16428c1b44f60b7c913dec0a6be18.jpg)
7
![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
8
![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
9
![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
10
![](https://image.cha138.com/20210612/d3d4739cb66f43b2bd5cab419e24dfc8.jpg)
下面来看一个小例子,首先要添加命名空间
using System.Runtime.Serialization.Formatters.Binary;
下面的代码将对象Person进行序列化并存储到一个文件中
Person me = new Person();
2![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
3
me.Age = 34;
4
me.WeightInPounds = 200;
5![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
6
Stream s = File.Open("Me.dat",FileMode.Create);
7![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
8
BinaryFormatter bf = new BinaryFormatter();
9![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
10
bf.Serialize(s,me);
11![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
12
s.Close();
2
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
3
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
4
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
5
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
6
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
7
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
8
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
9
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
10
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
11
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
12
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
然后再举一个并行化的例子
= File.Open("Me.dat",FileMode.Open);
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
BinaryFormatter bf = new BinaryFormatter();
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
object o = bf.Deserialize(s);
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
Person p = o as Person;
if(p != null)
Console.WriteLine("DeSerialized Person aged:{0} whight:{1}",p.Age,p.WeightInPounds);
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
s.Close();
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
如果需要对部分字段序列化部分不序列化时,我们可以按照如下设置实现
![](https://image.cha138.com/20210612/e8a46b8c120f4bf7b8d20a580834fd9f.jpg)
![](https://image.cha138.com/20210612/b949d1d7559049ea9645423338197bdf.jpg)
![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
![](https://image.cha138.com/20210612/f7a5a3cb56f44707840854969a7ce831.jpg)
![](https://image.cha138.com/20210612/7fb16428c1b44f60b7c913dec0a6be18.jpg)
![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
![](https://image.cha138.com/20210612/a89fe783b5ad46729d384b84cfbd9095.jpg)
![](https://image.cha138.com/20210612/d3d4739cb66f43b2bd5cab419e24dfc8.jpg)
2.
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/75de6ac12019450da560c24918ad338f.jpg)
![](https://image.cha138.com/20210612/a4b66bf47737487d9e3f6d8779e89ba0.jpg)
![](https://image.cha138.com/20210612/a4b66bf47737487d9e3f6d8779e89ba0.jpg)
![](https://image.cha138.com/20210612/a4b66bf47737487d9e3f6d8779e89ba0.jpg)
![](https://image.cha138.com/20210612/4046fa495727433a856e4559ea09dfe8.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
![](https://image.cha138.com/20210612/1cd82a7917b148d3bf2939378581a4d5.jpg)
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1735611
以上是关于C# Serializable的主要内容,如果未能解决你的问题,请参考以下文章