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

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 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;
        }
    }
}