1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.Serialization.Formatters.Binary;
5 using System.IO;
6 using System.Xml.Serialization;
7
8
9 public class SerializeObjectFactory
10 {
11
12 /// <summary>
13 /// 序列化为二进制文件
14 /// </summary>
15 /// <param name="o">序列化的对象</param>
16 /// <param name="fileFullPath">二进制文件完整路径</param>
17 public void SerializeToBinary(object o, string fileFullPath)
18 {
19 using (FileStream fs = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.Write))
20 {
21 BinaryFormatter formatter = new BinaryFormatter();
22 formatter.Serialize(fs, o);
23 }
24 }
25
26 /// <summary>
27 /// 从二进制文件反序列化对象
28 /// </summary>
29 /// <param name="fileFullPath">二进制文件完整路径</param>
30 /// <returns>Object类型</returns>
31 public object DeserializeFromBinary(string fileFullPath)
32 {
33 object o = null;
34 using (FileStream fs = new FileStream(fileFullPath, FileMode.Open))
35 {
36 fs.Seek(0, SeekOrigin.Begin);
37 BinaryFormatter formatter = new BinaryFormatter();
38 o = formatter.Deserialize(fs);
39 }
40
41 return o;
42 }
43
44 /// <summary>
45 /// 序列化为Xml文件
46 /// </summary>
47 /// <param name="o">序列化对象</param>
48 /// <param name="fileFullPath">Xml文件完整路径</param>
49 /// <param name="t">对象类型</param>
50 public void SerializeToXml(object o, string fileFullPath, Type t)
51 {
52 using (FileStream fs = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.Write))
53 {
54 XmlSerializer formatter = new XmlSerializer(t);
55 formatter.Serialize(fs, o);
56 }
57 }
58
59 /// <summary>
60 /// 从Xml文件反序列化对象
61 /// </summary>
62 /// <param name="fileFullPath">文件完整路径</param>
63 /// <param name="t">对象类型</param>
64 /// <returns>Object类型</returns>
65 public object DeserializeFromXml(string fileFullPath, Type t)
66 {
67 object o = null;
68 using (FileStream sr = new FileStream(fileFullPath, FileMode.Open))
69 {
70 sr.Seek(0, SeekOrigin.Begin);
71 XmlSerializer formatter = new XmlSerializer(t);
72 o = formatter.Deserialize(sr);
73 }
74
75 return o;
76 }
77
78
79 /// <summary>
80 /// 将对象序列化为Base64字符串
81 /// </summary>
82 /// <param name="o"></param>
83 /// <returns></returns>
84 public string SerializeToBase64(object o)
85 {
86 BinaryFormatter format = new BinaryFormatter();
87 MemoryStream ms = new MemoryStream();
88 format.Serialize(ms, o);
89
90 byte[] objectStream = ms.ToArray();
91
92 return Convert.ToBase64String(objectStream);
93 }
94
95 /// <summary>
96 /// 将Base64字符串序列化为对象
97 /// </summary>
98 /// <param name="base64Str"></param>
99 /// <returns></returns>
100 public object DesializeFromBase64(string base64Str)
101 {
102 BinaryFormatter format = new BinaryFormatter();
103
104
105 byte[] objectBytes = Convert.FromBase64String(base64Str);
106
107 MemoryStream ms = new MemoryStream(objectBytes);
108
109 return format.Deserialize(ms);
110
111 }
112 }
优质内容筛选与推荐>>
1、Extjs 基础篇—— Function 能在定义时就能执行的方法的写法 function(){...}()
2、Java 反射机制
3、EPPlusHelper
4、python 3.7 进阶 进程、线程、协程
5、[bzoj1086]王室联邦


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号