c# 用binary实现序列化和反序列化
直接用实例来说明序列化和反序列化:
namespace DynamicTest
{
class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person>();
Person p = new Person();
p.ID = 1;
p.Age = 12;
p.Name = "zhiqing";
p.Money = 120.3M;
list.Add(p);
string fileName = "C:\\Users\\zhiqing\\Desktop\\test.txt";
Stream fstream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器
binFormat.Serialize(fstream, list);
fstream.Close();
Console.WriteLine("c# 用二进制实现序列化");
////反序列化
try
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
BinaryFormatter binayFormat = new BinaryFormatter();
List<Person> li = (List<Person>)binayFormat.Deserialize(fs);
foreach (Person per in li)
{
Console.WriteLine("反序列化结果:ID :{0}", per.ID +","+ per.Name+"," + per.Age+"," + per.Money);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
Console.WriteLine("反序列化成功");
Console.ReadLine();
}
}
}
}
运行结果:

c# 用binary实现序列化和反序列化的更多相关文章
- 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树
原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...
- [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
- C# 序列化与反序列化之Binary与Soap无法对泛型List<T>进行序列化的解决方案
C# 序列化与反序列化之Binary与Soap无法对泛型List<T>进行序列化的解决方案 新建Console控制台项目项目,然后添加Team和Person 这2个类,如下: Team和P ...
- C# 序列化与反序列化Serialization之Json Xml Binary Soap JavaScript序列化
所谓的序列化其实就是把一个内存中的对象信息转化成一个可以持久化保存的形式,方便保存数据库和文件或着用于传输, 序列化的主要作用是不同平台之间进行通信与信息的传递保存等,常用的有序列化有Json Xml ...
- C#对象序列化与反序列化zz
C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍........................ ...
- C#对象序列化与反序列化
C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍.......................... ...
随机推荐
- YTU 2705:用重载求距离
2705: 用重载求距离. 时间限制: 1 Sec 内存限制: 128 MB 提交: 208 解决: 114 题目描述 使用函数重载的方法定义两个重名函数,分别求出整型数的两点间距离和浮点型数的两 ...
- 在word中doc与docx的区别是什么(整理)
在word中doc与docx的区别是什么(整理) docx 是Office2007使用的,是用新的基于XML的压缩文件格式取代了其目前专有的默认文件格式,在传统的文件名扩展名后面添加了字母x(即.do ...
- zookeeper单机安装
安装zookeeper步骤: 1,下载zookeeper http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/ 2,放到合适目录,解压 ...
- shopnc学习
---恢复内容开始--- 以前没有怎么接触过shopnc,感觉界面挺漂亮的,不过后来自己需要开发一个电商系统,就顺便参考了下,感觉构架垃圾的一塌糊涂.不过平时做这个系统二次开发的业务比较多,所以简单的 ...
- [Swift通天遁地]九、拔剑吧-(12)创建Preview-Transition图像预览界面
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- IIS: 响应消息的内容类型 text/html; charset=utf-8 与绑定(text/xml; charset=utf-8)的内容类型不匹配。如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法
以前好好的项目,突然出现了这个问题.一顿google后无果,有人说是程序池的原因,有人说是安全配置的原因,照着网上方法试了多次,还是没有解决.如下图: 突然发现了一片文章 (https://www.j ...
- overflow:解决 div的高度塌陷问题
高度塌陷是如何引起的? 解析: 当一个 div中所有的子 div都进行了浮动后,那么会出现该问题,那么解决方就是在父 div中 设置其 overflow:hidden;即可解决高度塌陷问题. 方式 ...
- T-SQL查询基础
今天来带大家了解下在sql server 中的查询机制 使用select语句进行查询 1.查询所有的数据行和列 select * from student 2.查询部分行和列 select scode ...
- Python爬取贴吧中的图片
#看到贴吧大佬在发图,准备盗一下 #只是爬取一个帖子中的图片 1.先新建一个scrapy项目 scrapy startproject TuBaEx 2.新建一个爬虫 scrapy genspider ...
- JS——stye属性
1.样式少的时候使用 this.parentNode.style.backgroundColor="yellow"; 2.style是对象 console.log(box.styl ...