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

推荐订阅源

B
Blog RSS Feed
Martin Fowler
Martin Fowler
爱范儿
爱范儿
IT之家
IT之家
Last Week in AI
Last Week in AI
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
The Cloudflare Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog

博客园 - teacherzj

EF---延迟加载技术 Entity Framework 1 委托 xml 可扩展的标记语言 单例模式 集合,对象初始化器 序列化与反序列化 进程与线程 winform基础 MD5加密 接口 重写ToString()方法 密封类 部分类 值类型和引用类型 简单工厂模式 C#中的访问修饰符 多态 FileStream、StreamReader和StreamWriter
序列化和反序列化
teacherzj · 2014-12-03 · via 博客园 - teacherzj

序列化和反序列化
序列化就是将对象转化为二进制
反序列化就是二进制转换为对象
作用:传输数据
序列化过程:
1)将这个类标记为可以被序列化的。


Person p=new Persion();
p.Name="张三";
p.Age=19;
p.Gender="男";
using(FileStream fsWrite=new FileStream(@"c:\new.txt",FileMode.OpenOrCreate,FileAccess.Write))
{
//开始序列化对象
BinaryFormatter bf=new BinaryFormatter();
bf.Serialize(fsWrite,p);
}
Console.Write("序列化成功!");

//接收对方发送过来的二进制,反序列化成对象。
Person p;
using(FileStream fsRead=new FileStream(@"c:\new.txt",FileMode.OpenOrCreate,FileAccess.Read))
{
BinaryFormatter bf=new BinaryFormatter();
p=(Person)bf.Deserialize(fsReader);
}
Console.Write(p.Name);

[Serializable]
public class Persion
{
private string _name;
public String Name
{
get{retun _name;}
set{_name=value;}
}
private int _age;
public int Age
{
get{return _age;}
set{_age=value;}
}
}