C#里serializable是什么意思?

如题所述

序列化

序列化是指存储和获取磁盘文件、内存或其他地方中的对象。在序列化时,所有的实例数据都保存到存储介质上,在取消序列化时,对象会被还原,且不能与其原实例区别开来。

只需给类添加Serializable属性,就可以实现序列化实例的成员。

并行化是序列化的逆过程,数据从存储介质中读取出来,并赋给类的实例变量。

[Serializable]
public class Person
{
public Person()
{
}

public int Age;
public int WeightInPounds;
}
下面来看一个小例子,首先要添加命名空间

using System.Runtime.Serialization.Formatters.Binary;
下面的代码将对象Person进行序列化并存储到一个文件中
Person me = new Person();
me.Age = 34;
me.WeightInPounds = 200;
Stream s = File.Open("Me.dat",FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(s,me);
s.Close();

如果需要对部分字段序列化部分不序列化时,我们可以按照如下设置实现
[Serializable]
public class Person
{
public Person()
{ }
public int Age;
[NonSerialized]
public int WeightInPounds;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-12
序列化后的对象可以在网络之间进行传输!
第2个回答  2012-04-12
序列化。将ojbect用字符的形式储存起来。