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

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - StephenJu

EntLib--Unity Application Block 1.x DevExpress杂项 .Net异步机制 简单的异步调用 按s1_Name+uuid为一组拆分DS 枚举 分割dataset:待改进... DataGridViewComboBoxColumn的使用 datagridview数据验证 通过关键字查找到dgv相关记录后定位 禁止一个程序启动多个实例 将文本文件的内容写进某个表中 获取Assembly的运行路径 获取ArrayList中的数据(foreach) 关于DataTable里大批量查找的更快速的方法 抽象类 IO DynamicCreateMenu DataTable的简单方法
序列化
StephenJu · 2008-06-17 · via 博客园 - StephenJu

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;namespace Sys
{
    [Serializable] 
//指示一个类可以序列化
    public class Element
    {
        
//复制对象
        public object DeepClone()
        {
            
object result = null;
            
using (MemoryStream ms = new MemoryStream()) //内存流
            {
                BinaryFormatter bf 
= new BinaryFormatter();//以二进制格式序列化对象
                bf.Serialize(ms, this);//将对象序列化为内存流
                ms.Seek(0, SeekOrigin.Begin);
                result 
= bf.Deserialize(ms); //将内存流反序列化为对象
            }
            
return result;
        }
//保存对象到文件
        
//(对象序列化到ms)→(ms写入byte[])→(byte[]写入流)
        public void WriteInstanceToFile(string fileName)
        {
            
using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf 
= new BinaryFormatter();
                bf.Serialize(ms, 
this);
                ms.Seek(
0, SeekOrigin.Begin);
                
byte[] buffer = new byte[ms.Length];
                ms.Read(buffer, 
0, buffer.Length);
                ms.Seek(
0, SeekOrigin.Begin);

                FileStream stream 

= new FileStream(fileName, FileMode.Create);
                stream.Write(buffer,
0,buffer.Length);
            }
        }
//从文件中读取对象
        public static object ReadInstanceFromFile(string fileName)
        {
            
object result = null;
            FileStream stream 
= new FileStream(fileName, FileMode.Open);//以二进制格式序列化对象
            BinaryFormatter bf = new BinaryFormatter();
            result 
= bf.Deserialize((Stream)stream); //流反序列化为对象
            stream.Close();
            
return result;
        }
    }
}