序列化将对象转换为字节流,并将其转换为可以写入流的形式。这样做是为了将其保存到内存、文件或数据库中。
可以执行以下序列化操作:
二进制序列化所有成员,甚至只读成员,都会被序列化。
xml序列化它将对象的公共字段和属性序列化为符合特定xml模式定义语言文档的xml流。
让我们看一个例子。首先设置流:
filestream fstream = new filestream("d:\ew.txt", filemode.openorcreate);binaryformatter formatter=new binaryformatter();
现在创建该类的一个对象并调用具有三个参数的构造函数 -
employee emp = new employee(030, "tom", “operations”);
执行序列化。
formatter.serialize(fstream, emp);
反序列化是序列化的逆过程,通过它,您可以从字节流中读取对象。
formatter.deserialize(fstream);
以上就是c# 中的序列化和反序列化的详细内容。
