惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Vulnerabilities – Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Proofpoint News Feed
G
GRAHAM CLULEY
P
Privacy International News Feed
The Hacker News
The Hacker News
Forbes - Security
Forbes - Security
U
Unit 42
N
News and Events Feed by Topic
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cisco Blogs
A
About on SuperTechFans
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Docker
I
Intezer
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
F
Full Disclosure
S
Secure Thoughts
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
W
WeLiveSecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Project Zero
Project Zero
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
S
Security Affairs
AWS News Blog
AWS News Blog
H
Help Net Security
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
Vercel News
Vercel News
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Register - Security
The Register - Security
S
Schneier on Security
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 最新话题
T
Tor Project blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog

博客园 - BlackPhoenix

SQL Server将数据导出SQL脚本的方法 国内Asp.net博客系统收集和简单介绍 1999-2006中国统计年鉴 CodeSmith.Pro.4.1.2破解和注册 office2007的电脑上,右键菜单新建“office2003”文档的办法! 农行动态口令卡问题解决方案(Key:Vista,IE7,证书已锁定,438对象不支持此属性或方法) VS2005Team版如何进行单元测试 解决vista系统部分CHM打不开,“无法正常显示”的问题 C#开源资源大汇总 Windows Vista中微软拼音输入法2007默认是英文标点问题解决 string.format()字符传格式化时特殊字符要进行转义 C#:String.Format数字格式化输出 ASP.NET-GridView的分页功能 [coll]ASP.NET刷新页面的六种方法 [转]DataFormatString的使用 [转]中国大学金融专业排名 SQL SERVER的字段类型说明及简单比较 两个月没编程了,现在开工! 结合委托与AJAX,实现无刷新确认对话框的开源用户控件
序列化- 使用BinaryFormatter进行序列化
BlackPhoenix · 2009-01-23 · via 博客园 - BlackPhoenix

Posted on 2009-01-23 15:40  BlackPhoenix  阅读(647)  评论()    收藏  举报


可以使用属性(Attribute)将类的元素标为可序列化的(Serializable)和不可被序列化的(NonSerialized).NET中有两个类实现了IFormatter借口的类中的SerializeDeserialize方法:BinaryFormatterSoapFormatter。这两个类的区别在于数据流的格式不同。使用BinaryFormatter进行序列化
在下面这个例子中我们建立一个自定义类型(Insect)集合,使用BinaryFormatter将它们写到二进制文件,然后再将他们读回。
注:以下程序需要导入一些命名空间:
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

我们使用一个标准属性将整个Insect类声明为可序列化的。但是因为一个字段被声明为不可序列化,所以这个字段不能被持久化。我们先做一个试验,我们只实例化一个Insect对象,创建一个文件,然后使用BinaryFormatter对象和Serialize方法写出这个Insect对象:

如果在Visual Studio打开Insect.bin文件就会看到以下内容:
FBinaryFormatter, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Insect name Meadow Brown
(由于我没有截图软件,所以这只是部分内容)
我们可以注意到并没有id字段,因为它没有被序列化。现在,我们增加几个Insect对象。

下面是这个程序的输出:
Meadow Brown:0
Marsh Fritillary:0
Speckled Wood:0
Milkweed:0

id值是0,其原因是很明显的(它在foreach循环中构造Insect的期间被初始化为0)。
注意,我们非常小心地先读回一个Insect对象 - 在读回集合之前已经被序列化到文件的对象。
另外,在我们使用Deserialize时,必须对返回的对象进行类型转换,因为这个方法返回一个一般性的对象。

在后面添加的集合中有三个Insect的数据,这节省了一些开销,因为只需要为第一列的Insect记录Insect类的类型信息。
另外一个有意思的地方是,序列化机制显然能够读写列中的私有字段。