平时开发中经常要用到序列化和反序列化技术,写了一个工具类实现,共享给大家。
直接上代码


1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Gren.FrameWork.Common
6 {
7 /// <summary>
8 /// 序列化、反序列化工具
9 /// </summary>
10 public class SerializationUtility
11 {
12 /// <summary>
13 /// 将对象序列化为XML格式的字符串
14 /// </summary>
15 public static string SerializeByXml<T>(T obj)
16 {
17 string xml = string.Empty;
18
19 if (obj != null)
20 {
21 StringBuilder sb = new StringBuilder();
22
23 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
24
25 using (System.IO.StringWriter sw = new System.IO.StringWriter(sb))
26 {
27 xs.Serialize(sw, obj);
28 }
29 xml = sb.ToString();
30 }
31
32 return xml;
33 }
34
35 /// <summary>
36 /// 将XML格式的字符串反序列化为对象实例
37 /// </summary>
38 public static T DeserializeByXml<T>(string xml)
39 {
40 object obj = null;
41
42 if (!string.IsNullOrEmpty(xml))
43 {
44 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
45
46 using (System.IO.StringReader sr = new System.IO.StringReader(xml))
47 {
48 obj = xs.Deserialize(sr);
49 }
50 }
51 return (T)obj;
52 }
53
54 /// <summary>
55 /// 将对象序列化为二进制数组
56 /// </summary>
57 public static byte[] SerializeToByte<T>(T obj)
58 {
59 byte[] buffer = null;
60
61 if (obj != null)
62 {
63 System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
64
65 using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
66 {
67 fmt.Serialize(ms, obj);
68 buffer = ms.ToArray();
69 }
70 }
71
72 return buffer;
73 }
74
75 /// <summary>
76 /// 将二进制数组反序列化为对象实例
77 /// </summary>
78 public static T DeserializeFromByte<T>(byte[] buffer)
79 {
80 object obj = null;
81
82 if (buffer != null)
83 {
84 System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
85
86 using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer))
87 {
88 obj = fmt.Deserialize(ms);
89 }
90 }
91
92 return (T)obj;
93 }
94
95 /// <summary>
96 /// 将对象序列化为base64String
97 /// </summary>
98 public static string SerializeByBinary<T>(T obj)
99 {
100 string base64String = string.Empty;
101
102 if (obj != null)
103 {
104 byte[] buffer = SerializeToByte<T>(obj);
105 base64String = Convert.ToBase64String(buffer);
106 }
107
108 return base64String;
109 }
110
111 /// <summary>
112 /// 将base64String反序列化为对象实例
113 /// </summary>
114 public static T DeserializeByBinary<T>(string base64String)
115 {
116 object obj = null;
117
118 if (!string.IsNullOrEmpty(base64String))
119 {
120 byte[] buffer = Convert.FromBase64String(base64String);
121 obj = DeserializeFromByte<T>(buffer);
122 }
123
124 return (T)obj;
125 }
126
127 }
128 }
129
命名有点不规范,主要是以前没注意,后来在项目中使用了,再改的话会影响现有项目。大家要用的话自己改改名称,代码很简单。
有什么地方不合理或者可以优化的请给我留言,谢谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述