直接用实例来说明序列化和反序列化:

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实现序列化和反序列化的更多相关文章

  1. 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树

    原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...

  2. [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 ...

  3. [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 ...

  4. [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)

    描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...

  5. 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化

    序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...

  6. C# 序列化与反序列化之Binary与Soap无法对泛型List<T>进行序列化的解决方案

    C# 序列化与反序列化之Binary与Soap无法对泛型List<T>进行序列化的解决方案 新建Console控制台项目项目,然后添加Team和Person 这2个类,如下: Team和P ...

  7. C# 序列化与反序列化Serialization之Json Xml Binary Soap JavaScript序列化

    所谓的序列化其实就是把一个内存中的对象信息转化成一个可以持久化保存的形式,方便保存数据库和文件或着用于传输, 序列化的主要作用是不同平台之间进行通信与信息的传递保存等,常用的有序列化有Json Xml ...

  8. C#对象序列化与反序列化zz

      C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍........................ ...

  9. C#对象序列化与反序列化

    C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍.......................... ...

随机推荐

  1. NoSQL 世界交换数据的事实标准

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/data-in-data-out.html An object is a lan ...

  2. Android 体系结构介绍

    转自:http://blog.sina.com.cn/s/blog_4bc996c40100fawo.html 第一.操作系统层(OS)第二.各种库(Libraries)和Android 运行环境(R ...

  3. CF732 F Tourist Reform——边双连通分量

    题目:http://codeforces.com/contest/732/problem/F 首先把边双缩点,边双内部 dfs 一个顺序一定是可以从每个点走到边双内部所有点的,因为它是以环为基本单位: ...

  4. Rabbit-service Message queue MQ 验证 校验

    事务概要================================================================================================ ...

  5. IBatis异常: Cannot find class: VARCHAR

    今天再项目里添加新功能时,突然爆出 org.springframework.beans.factory.BeanCreationException: Error creating bean with ...

  6. Rank of Tetris(topsort)

    http://acm.hdu.edu.cn/showproblem.php?pid=1811 #include <stdio.h> #include <string.h> #i ...

  7. 关于CSS中float的两点心得以及清除浮动的总结

    对一个元素运用float后,该元素将脱离正常文档流,这意味着: 1. 运用float后,该元素不再影响父元素的高度,如果一个元素的所有子元素都是float的话,那么该元素的高度是0,这样后面元素渲染的 ...

  8. [Swift通天遁地]九、拔剑吧-(16)搭建卡片页面:Card Peek/Pop动态切换界面

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. Linux 下文本查找技巧你掌握了吗?

    前言 之前介绍过很多linux下查找相关的命令,例如<Linux中的文件查找技巧>,<find命令高级用法>,<如何查看linux中文件打开情况-lsof命令>等等 ...

  10. SQL SERVER 获取给定时间段内的所有日期列表

    declare @StartDate DATETIME = '2018/08/01'declare @EndDate DATETIME ='2018/09/27'SELECT CONVERT (VAR ...