• 序列化


    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 }
  • 相关阅读:
    IO操作之BIO、NIO、AIO
    IO之Socket网络编程
    this.getClass()和super.getClass()得到的是同一个类
    经济增长的背后
    SVN分支创建与合并
    .net类库里ListView的一个BUG
    VS2010调试技巧
    用C#代码编写的SN快速输入工具
    请教如何改善C#中socket通信机客户端程序的健壮性
    利用WebClient实现对Http协议的Post和Get对网站进行模拟登陆和浏览
  • 原文地址:https://www.cnblogs.com/xingbinggong/p/2095351.html
Copyright © 2020-2023  润新知