1.序列化一般有2种(XML和2进制),简单对象序列化

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Xml.Serialization; namespace Test
{
//对于XmlSerializer序列化,默认即使不使用特性Serializable,也是可以对对象进行序列化的,则BinaryFormatter不然一定要使用Serializable标记。
public partial class Form1 : Form
{
//XmlSerializer是XML序列化
XmlSerializer xs = new XmlSerializer(typeof(Student));
//二进制序列化
BinaryFormatter b = new BinaryFormatter();
Student student = new Student() { Name = "小明", Age = };
public Form1()
{
InitializeComponent();
//xml序列化
using (Stream stream = new FileStream("d:\\Student.xml", FileMode.Create, FileAccess.Write, FileShare.Read))
{
xs.Serialize(stream, student);
}
//xml反序列化
using (FileStream fs = new FileStream("d:\\Student.xml", FileMode.Open, FileAccess.Read))
{
Student student = (Student)xs.Deserialize(fs);
}
//二进制序序列化
using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Create))
{
BinaryFormatter b = new BinaryFormatter();
//序列化类要加[Serializable]特性
b.Serialize(fileStream, student);
}
//二进制序反序列化
using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Open, FileAccess.Read))
{
BinaryFormatter bf = new BinaryFormatter();
student = (Student)bf.Deserialize(fileStream);
}
} }
} [Serializable]
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}

2.复杂对象序列化

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization; namespace Test
{
public partial class Form1 : Form
{
//XmlSerializer是XML序列化
XmlSerializer xs = new XmlSerializer(typeof(TeacherStudent));
//二进制序列化
BinaryFormatter b = new BinaryFormatter();
TeacherStudent teacherStudent = new TeacherStudent(); Teacher teacher = new Teacher() { Name = "王老师", Age = };
Student student = new Student() { Name = "小明", Age = };
public Form1()
{
InitializeComponent();
//xml序列化
using (Stream stream = new FileStream("d:\\Student.xml", FileMode.Create, FileAccess.Write, FileShare.Read))
{
teacherStudent.Teacher = teacher;
teacherStudent.Student = student;
xs.Serialize(stream, teacherStudent);
}
//xml反序列化
using (FileStream fs = new FileStream("d:\\Student.xml", FileMode.Open, FileAccess.Read))
{
teacherStudent = null;
teacherStudent = (TeacherStudent)xs.Deserialize(fs);
}
//二进制序序列化
using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Create))
{
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, teacher);
b.Serialize(fileStream, student);
}
//二进制序反序列化
using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Open, FileAccess.Read))
{
teacher = null;
student = null;
BinaryFormatter bf = new BinaryFormatter();
teacher = (Teacher)bf.Deserialize(fileStream);
student = (Student)bf.Deserialize(fileStream);
}
} }
} [Serializable]
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
} [Serializable]
public class Teacher
{
public string Name { get; set; }
public int Age { get; set; }
} [Serializable]
public class TeacherStudent
{
public Teacher Teacher { get; set; }
public Student Student { get; set; }
}

3. 控制序列化/反序列化前后的数据

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms; namespace Test
{
public partial class Form1 : Form
{
//XmlSerializer是XML序列化
BinaryFormatter b = new BinaryFormatter();
Student student = new Student() { Name = "小明", Age = };
public Form1()
{
InitializeComponent();
//二进制序序列化
using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Create))
{
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, student);
}
//二进制序反序列化
using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Open, FileAccess.Read))
{
BinaryFormatter bf = new BinaryFormatter();
student = (Student)bf.Deserialize(fileStream);
}
} }
} [Serializable]
public class Student
{
public string Name { get; set; }
public int Age { get; set; } [OnSerializing()]
internal void OnSerializingMethod(StreamingContext context)
{
//格式化器在序列化开始之前调用此方法。
Console.WriteLine("OnSerializing格式化器在序列化开始之前调用此方法");
} [OnSerialized()]
internal void OnSerializedMethod(StreamingContext context)
{
//格式化器在序列化后调用此方法。
Console.WriteLine("OnSerialized格式化器在序列化后调用此方法");
} [OnDeserializing()]
internal void OnDeserializingMethod(StreamingContext context)
{
//格式化器在反序列化开始之前调用此方法。
Console.WriteLine("OnDeserializing格式化器在反序列化开始之前调用此方法");
} [OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context)
{
//格式化器在序列化开始之前调用此方法。
Console.WriteLine("OnDeserialized格式化器在序列化开始之前调用此方法");
}
}

C#序列化的更多相关文章

  1. 【.net 深呼吸】序列化中的“引用保留”

    假设 K 类中有两个属性/字段的类型相同,并且它们引用的是同一个对象实例,在序列化的默认处理中,会为每个引用单独生成数据. 看看下面两个类. [DataContract] public class 帅 ...

  2. 【.net 深呼吸】设置序列化中的最大数据量

    欢迎收看本期的<老周吹牛>节目,由于剧组严重缺钱,故本节目无视频无声音.好,先看下面一个类声明. [DataContract] public class DemoObject { [Dat ...

  3. 用dubbo时遇到的一个序列化的坑

    首先,这是标题党,问题并不是出现在序列化上,这是报错的一部分: Caused by: com.alibaba.dubbo.remoting.RemotingException: Failed to s ...

  4. Unity 序列化

    Script Serialization http://docs.unity3d.com/Manual/script-Serialization.html 自定义序列化及例子: http://docs ...

  5. Unity 序列化 总结

    查找了 Script Serialization http://docs.unity3d.com/Manual/script-Serialization.html 自定义序列化及例子: http:// ...

  6. [C#] C# 知识回顾 - 序列化

    C# 知识回顾 -  序列化 [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5902005.html 目录 序列化的含义 通过序列化保存对象数据 众 ...

  7. Newtonsoft.Json设置类的属性不序列化

    参考页面: http://www.yuanjiaocheng.net/webapi/parameter-binding.html http://www.yuanjiaocheng.net/webapi ...

  8. C# 序列化与反序列化几种格式的转换

    这里介绍了几种方式之间的序列化与反序列化之间的转换 首先介绍的如何序列化,将object对象序列化常见的两种方式即string和xml对象; 第一种将object转换为string对象,这种比较简单没 ...

  9. Netty实现高性能RPC服务器优化篇之消息序列化

    在本人写的前一篇文章中,谈及有关如何利用Netty开发实现,高性能RPC服务器的一些设计思路.设计原理,以及具体的实现方案(具体参见:谈谈如何使用Netty开发实现高性能的RPC服务器).在文章的最后 ...

  10. .Net深入实战系列—JSON序列化那点事儿

    序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer ...

随机推荐

  1. web前端基础知识-(五)jQuery

    通过之前的学习我们已经了解了html.css.javascript的相关知识:本次我们就共同学习进阶知识:jQuery~ 一.什么是jQuery? jQuery其实就是一个轻量级的javascript ...

  2. 博文Contents<1--到200—>

    ====================-------------- 前言:博客中的随笔文章.并非都是笔者的原创文章.有些是听别人说的.有些是书上摘录的.有些是百度的.有些是别人博客的文章.有些是自己 ...

  3. win tomcat

    D:\tomcat8080\binstartup.bat rem ------------------------------------------------------------------- ...

  4. [git] warning: LF will be replaced by CRLF | fatal: CRLF would be replaced by LF

    遇到这两个错误,是因为Git的换行符检查功能. core.safecrlf Git提供了一个换行符检查功能(core.safecrlf),可以在提交时检查文件是否混用了不同风格的换行符.这个功能的选项 ...

  5. 红米2A高配刷机记录

    2014816 机型:红米2A高配版 设备型号:2014816 CPU:高通 线刷:fastboot平台 http://192.168.7.118/MesReports/Reports/Cutting ...

  6. 移动端Web开发调试之Chrome远程调试(Remote Debugging)

    比如手机钉钉调试页面,下面是一位同学整理的链接: http://blog.csdn.net/freshlover/article/details/42528643/ 如果inspect 后,一直空白, ...

  7. git gui 还原部分提交文件

    有时候用git提交文件的时候会一起提交了多个文件,但是突然后悔了,想把其中一个文件撤销提交,其他文件不做修改.这个时候该怎么办呢? 我觉得有很多办法,比如可以先checkout到上次的提交,然后复制要 ...

  8. vim 标签页 tabnew 等的操作命令

    对于vim这个 ide来说, 单纯的用 多子窗口 来操作, 感觉还是不够的, 还要结合标签页tab pages来,才能更好的操作. 所有关于标签 的 命令行 命令都是 以 :tab开始的, 可以用ta ...

  9. C# Activator.CreateInstance()方法使用

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  10. 【转载】Unity中矩阵的平移、旋转、缩放

    By:克森 简介 在这篇文章中,我们将会学到几个概念:平移矩阵.旋转矩阵.缩放矩阵.在学这几个基本概念的同时,我们会用到 Mesh(网格).数学运算.4x4矩阵的一些简单的操作.但由于克森也是新手,文 ...