23人参与 • 2025-08-19 • Asp.net
对象在程序运行时存储在内存中,程序关闭后会被垃圾回收机制销毁,无法保留状态。若需在程序再次运行时恢复对象状态,需将对象信息持久化到磁盘(如文件)中。
原始保存方式的局限
直接将对象属性按顺序写入文本文件(如people.obj
),通过顺序读取还原对象,存在明显缺点:
为解决原始保存方式的问题,引入 “序列化” 和 “反序列化” 机制:
将对象转换为二进制流,效率高、体积小,但可读性差(二进制无法直接看懂)。
核心组件
binaryformatter
:位于system.runtime.serialization.formatters.binary
命名空间,负责二进制序列化与反序列化。
使用步骤
[serializable]
特性(否则序列化失败);filestream
指定存储路径和模式(如filemode.create
创建文件);binaryformatter
对象;serialize()
(写入对象)或deserialize()
(读取对象);示例代码
// 1. 定义可序列化的类 [serializable] // 必须添加此特性 public class people { public string name { get; set; } public int age { get; set; } public string sex { get; set; } public string birth { get; set; } } // 2. 序列化过程 public void binaryserialize() { people people = new people() { name = "吴凡", age = 18, sex = "男", birth = "2005-01-01" }; // 创建文件流 using (filestream fs = new filestream("people.bin", filemode.create)) { binaryformatter bf = new binaryformatter(); bf.serialize(fs, people); // 将对象写入流 } } // 3. 反序列化过程 public void binarydeserialize() { using (filestream fs = new filestream("people.bin", filemode.open)) { binaryformatter bf = new binaryformatter(); // 反序列化并转换为people类型 people people = bf.deserialize(fs) as people; console.writeline($"姓名:{people.name},年龄:{people.age}"); } }
特点
json(javascript object notation)是一种轻量级文本格式,易读易写,跨平台兼容性强(支持多语言),是目前最常用的序列化格式之一。
json 格式示例:
{ "name":"吴凡", "age":18, "sex":"男", "birth":"2005-01-01" }
方式 1:原生datacontractjsonserializer
依赖system.runtime.serialization.json
命名空间,需手动标记类和属性。
使用步骤
[datacontract]
,需序列化的属性添加[datamember]
;datacontractjsonserializer
需指定对象类型(如typeof(people)
);writeobject()
(序列化)或readobject()
(反序列化)。示例代码
// 1. 定义数据契约类 using system.runtime.serialization; [datacontract] // 标记为数据契约类 public class people { [datamember] // 标记为需序列化的成员 public string name { get; set; } [datamember] public int age { get; set; } [datamember] public string sex { get; set; } [datamember] public string birth { get; set; } } // 2. 序列化 public void jsonserialize_native() { people people = new people() { name = "吴凡", age = 18, sex = "男", birth = "2005-01-01" }; using (filestream fs = new filestream("people.json", filemode.create)) { datacontractjsonserializer jsonserializer = new datacontractjsonserializer(typeof(people)); jsonserializer.writeobject(fs, people); // 写入json } } // 3. 反序列化 public void jsondeserialize_native() { using (filestream fs = new filestream("people.json", filemode.open)) { datacontractjsonserializer jsonserializer = new datacontractjsonserializer(typeof(people)); people people = jsonserializer.readobject(fs) as people; // 读取json并转换为对象 console.writeline($"姓名:{people.name},年龄:{people.age}"); } }
方式 2:第三方newtonsoft.json(推荐)
最流行的 json 处理库,简化序列化逻辑,无需标记特性,支持复杂对象和集合。
使用步骤
using newtonsoft.json;
;jsonconvert.serializeobject(对象)
生成 json 字符串,写入文件;jsonconvert.deserializeobject<类型>(字符串)
还原对象。示例代码
using newtonsoft.json; // 1. 定义普通类(无需任何特性) public class people { public string name { get; set; } public int age { get; set; } public string sex { get; set; } public string birth { get; set; } } // 2. 序列化 public void jsonserialize_newtonsoft() { people people = new people() { name = "吴凡", age = 18, sex = "男", birth = "2005-01-01" }; // 转换对象为json字符串 string jsonstr = jsonconvert.serializeobject(people); // 写入文件 file.writealltext("people.json", jsonstr); } // 3. 反序列化 public void jsondeserialize_newtonsoft() { // 读取json字符串 string jsonstr = file.readalltext("people.json"); // 转换为对象 people people = jsonconvert.deserializeobject<people>(jsonstr); console.writeline($"姓名:{people.name},年龄:{people.age}"); }
json 序列化特点
序列化方式 | 核心工具类 | 可读性 | 跨平台性 | 易用性 | 适用场景 |
---|---|---|---|---|---|
二进制序列化 | binaryformatter | 差(二进制) | 差(仅限.net) | 中(需标记特性) | 本地高效存储、.net 内部通信 |
json 原生序列化 | datacontractjsonserializer | 好(文本) | 好 | 中(需标记特性) | 简单跨平台数据交换 |
json 第三方序列化 | newtonsoft.json | 好(文本) | 好 | 优(无需标记) | 多数场景(推荐),如接口通信、配置存储 |
原始文本保存因依赖顺序,仅适用于极简单场景;
二进制序列化适合.net 内部高效存储,不追求可读性时使用;
json 序列化(尤其是newtonsoft.json
)因易读、跨平台、易用,成为多数开发场景的首选。
到此这篇关于c#对象持久化的三种常用序列化方法总结的文章就介绍到这了,更多相关c#对象序列化内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论