基础命名空间:序列化 System.Runtime.Serialization
对象通常都有状态(state),从一个对象中抽取这种状态,不论是将它存储于某地,还是通过网络传送,这种抽取动作称为“将一个对象序列化”,而反向处理过程,从一个被序列化的状态重建一个对象即为反序列化。
序列化工作系由一个特定的格式化器(formatter)完成,每个格式化器都提供Serialize和Deserialize两个方法。当格式化器将某个对象序列化后,所得好结果被放入一个流(Stream)中,(所谓的流是字节序列的一个抽象概念)因此可以包容任何序列化格式。一对象被存储于一个流之中,对象的状态好久可以被存储于磁盘上(或者说被持久化(persistent))
对于一个可被序列化的类型,只需要给他表上[Serializable]特性,也可以只赋给某个特定的字段
NonSerialized 指明被标记的字段不可序列化
下面是自己练习的示例:
1.二进制序列化
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks; namespace SerializableTest
{
public class Program
{
static void Main(string[] args)
{
Goods good = new Goods();
good.name = "苹果";
good.price = ;
good.type = "水果"; string dir = System.AppDomain.CurrentDomain.BaseDirectory; //序列化
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(dir+"test.bin", FileMode.Create, FileAccess.Write);
formatter.Serialize(stream, good);
stream.Close();//必须关闭 //反序列化
IFormatter reformatter = new BinaryFormatter();
Stream filestream = new FileStream(dir+"test.bin", FileMode.Open, FileAccess.Read); //返回Object类型,必须强制转换
Goods newgood = (Goods)reformatter.Deserialize(filestream); Console.WriteLine(newgood.name);
Console.WriteLine(newgood.price);
Console.WriteLine(newgood.type);
Console.ReadLine(); }
} [Serializable]
public class Goods
{
/// <summary>
/// 名称
/// </summary>
public string name { get; set; } /// <summary>
/// 价格
/// </summary>
public double price { get; set; } /// <summary>
/// 分类
/// </summary>
public string type { get; set; }
}
}
上例使用二进制格式化器BinaryFormatter:System.Runtime.Serialization.Formatters.Binary;
注:Iformatter接口序列化对象时,只需要提供了Stream流对象就行了。将对象序列化到文件流(FileStream)、内存流(MemoryStream)、网络流(NetworkStream)都可以。
在测试 特新NonSerialized 时出现了点问题:“特性“NonSerialized”对此声明类型无效。它只对“field”声明有效”
由于C#3.0 的新特性get/set访问器,在编译的时候,编译器会自动为你生成对应的私有变量,变量名自动生成。
因此考虑 直接显示声明私有属性 private int number,并标注[NonSerialized]特性。
运行结果:
2.XML序列化
XML序列化,对象被以XML格式保存,XML序列化常常用在Web服务项目里(最近的项目里看到有模块用到,所以自己学习一下)
System.Xml.Serialization命名空间:含有使用XML序列化所需要的类和功能
XmlSerializer类,提供序列化Serialeze()和反序列话Deserialize()方法。
XmlIgnore属性,让XmlSerializer类跳过不序列化的成员(XML序列化 Serializable和NoSerialized属性将被忽略,而是使用XmlIgnore属性,它的作用与NoSerialized类似)
例如:
/// <summary>
/// 分类
/// </summary>
[XmlIgnore]
public string type { get; set; }
Goods good = new Goods();
good.name = "苹果";
good.price = ;
good.type = "水果";
good.Number = ; string dir = System.AppDomain.CurrentDomain.BaseDirectory; //序列化
XmlSerializer formatter = new XmlSerializer(typeof(Goods));
FileStream stream = new FileStream(dir + "test.bin", FileMode.Create, FileAccess.Write);
formatter.Serialize(stream, good);
stream.Close();//必须关闭 //反序列化
XmlSerializer reformatter = new XmlSerializer(typeof(Goods));
FileStream filestream = new FileStream(dir + "test.bin", FileMode.Open, FileAccess.Read); //返回Object类型,必须强制转换
Goods newgood = (Goods)reformatter.Deserialize(filestream); Console.WriteLine("名称:" + newgood.name);
Console.WriteLine("价格:" + newgood.price);
Console.WriteLine("种类:" + newgood.type);
Console.WriteLine("数量:" + newgood.Number);
Console.ReadLine();
持久化后的XML数据
<?xml version="1.0"?>
<Goods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<name>苹果</name>
<price></price>
<Number></Number>
</Goods>
可以发现加了[XmlIgnore]特性的type字段没有被序列化
注:public int Number被序列化了,private int number 没有被序列化,据说XML序列化 private类型字段不能被序列化,且元素的属性必须为读/写属性。
基础命名空间:序列化 System.Runtime.Serialization的更多相关文章
- 基础命名空间:序列化_自定义序列化 System.Runtime.Serialization
( (From Msdn) 自定义序列化是控制类型的序列化和反序列化的过程,通过控制序列化,可以确保序列化兼容性.换而言之,在不中断类型核心功能的情况下,可在类型的不同版本之间序列化和反序列化. 重 ...
- System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization.dll 中发生
异常信息: “System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization. ...
- System.Runtime.Serialization.cs
ylbtech-System.Runtime.Serialization.cs 允许对象控制其自己的序列化和反序列化过程. 1.返回顶部 1. #region 程序集 mscorlib, Versio ...
- 找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”的解决办法
找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”.的解决办法站点发布后部署到了两 ...
- 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则
在.NET 4.0下使用最新版本的log4Net 1.2.10,会遇到下面这样的错误: 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObject ...
- 找不到System.Runtime.Serialization.Json的解决方案
System.ServiceModel System.ServiceModel.Web System.Runtime.Serialization 三者均要添加引用
- 引用System.Runtime.Serialization.Json
vs2012下,重新添加一次System.Runtime.Serialization的引用
- csharp:.net 3.5 using System.Runtime.Serialization.Json read json
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newtonsoft.Json.JsonConvert and System.Text.Json.JsonSerializer.Serialize
In .net core 3.0 using System;using System.Collections.Generic;using System.Collections;using System ...
随机推荐
- 通过maven创建自己的archetype
最近项目组做好一套框架,为了推广需要创建一些空白项目给项目组使用,因为所有的空白项目里面的配置基本上都是一样的,为了减少重复工作,想通过maven创建一个自己的archetype,于是在网上大致搜了一 ...
- C#操作Flash动画
对于在C#开发的过程中没有接触过Flash相关开发的人员来说,没有系统的资料进行学习,那么这篇文档针对于初学者来说是很好的学习DEMO. 本文章中的DEMO实现了C#的COM控件库中本来就带有对fla ...
- jquery的Post方法$.post()
$.post是jquery自带的一个方法,使用前需要引入jquery.js 语法:$.post(url,data,callback,type); url(必须):发送请求的地址,String类型 da ...
- win8 Pro 64位在 UEFI模式下Ghost系统 备份 恢复
一:在win8 安装U 盘中 1. 新建 “Ghost” 文件夹 2. 将下载的Ghost64.exe 文件拷贝到文件夹 二: 启动的时候 按下F12 选择 HDDUSB 1.Windows 安装 ...
- winform textbox 的自动实现功能
好久没写博客了,主要是太懒了,之前因为做bs的比较多现在想转cs端了,虽然现在做cs也一年了,可接触的东西太过零碎了,以至于感觉这一年好像什么都没有学到.估计是因为学了之后没有记录,不扎实,然后又忘记 ...
- java中连接postgresql基本代码
try { Class.forName( "org.postgresql.Driver" ).newInstance(); String url = "jdbc:post ...
- php爬虫的两种思路
写php爬虫可能最大的问题就是php脚本执行时间的问题了,对于这个问题,我找到了两种解决方法. 第一种通过代码set_time_limit(0)或者ini_set("max_executio ...
- 将图片以Blob格式存入数据库,再通过Servlet显示到界面
1:为了方便测试,直接将1.png图片存入到数据库中. public static void storePicBlog() throws FileNotFoundException, SQLExcep ...
- DELL配置信息
CPU详情CPU厂商 英特尔CPU (英特尔)Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHzCPU核心数 2CPU默认频率 2400 MhzCPU外频 533 MHz ...
- POJ Oulipo (KMP)
题目大意 : 在一个字符串中找出目标单词的个数 代码: #include<iostream> #include<cstdio> #include<cstdlib> ...