2023-05-27 Unity 2进制4——类对象的序列化与反序列化

文章详细介绍了C#中如何使用[BinarayFormatter]进行对象的序列化和反序列化。通过添加[System.Serializable]特性声明可序列化类,然后利用MemoryStream或FileStream进行数据的存储和读取。示例中展示了如何将Person类实例转换为字节数组并保存到文件,以及如何从文件中恢复反序列化的对象。

一、序列化

(一)声明类对象

​ 如果要使用 C# 自带的序列化 2 进制方法,申明类时需要添加[System.Serializable]特性。

[System.Serializable]
public class Person
{
    public int age = 1;
    public string name = "xxx";
    public int[] ints = new int[] { 1, 2, 3, 4, 5 };
    public List<int> list = new List<int>() { 1, 2, 3, 4 };
    public Dictionary<int, string> dic = new Dictionary<int, string>() { { 1, "123" }, { 2, "1223" }, { 3, "435345" } };
    public StructTest st = new StructTest(2, "123");
    public ClassTest ct = new ClassTest();
}

[System.Serializable]
public struct StructTest
{
    public int i;
    public string s;

    public StructTest(int i, string s) {
        this.i = i;
        this.s = s;
    }
}

[System.Serializable]
public class ClassTest
{
    public int i = 1;
}

(二)方法一:使用内存流获得字节数组

​ 主要用于得到字节数组 可以用于网络传输。

  • 内存流对象

    类名:MemoryStream

    命名空间:System.IO

  • 进制格式化对象

    类名:BinaryFormatter

    命名空间:System.Runtime.Serialization.Formatters.Binary

  • 主要方法:序列化方法 Serialize

    将对象序列化为给定流。

    public void Serialize(Stream serializationStream, object obj);

Person p = new Person();

using (MemoryStream ms = new MemoryStream()) {
    // 2进制格式化程序
    BinaryFormatter bf = new BinaryFormatter();
    // 序列化对象 生成2进制字节数组 写入到内存流当中
    bf.Serialize(ms, p);
    // 得到对象的2进制字节数组
    byte[] bytes = ms.GetBuffer();
    // 存储字节
    File.WriteAllBytes(Application.dataPath + "/Lesson5.tang", bytes);
    // 关闭内存流
    ms.Close();
}

(三)方法二:使用文件流进行存储

​ 主要用于存储到文件中。

using (FileStream fs = new FileStream(Application.dataPath + "/Lesson5_2.tang", FileMode.OpenOrCreate,
           FileAccess.Write)) {
    // 2进制格式化程序
    BinaryFormatter bf = new BinaryFormatter();
    // 序列化对象 生成2进制字节数组 写入到内存流当中
    bf.Serialize(fs, p);
    fs.Flush();
    fs.Close();
}

​ C# 提供的类对象 2 进制序列化主要类是 BinaryFormatter
​ 通过其中的序列化方法即可进行序列化生成字节数组

二、反序列化

(一)反序列化文件数据

  • 主要方法:反序列化方法 Deserialize

    将指定的流反序列化为对象图

    public object Deserialize(Stream serializationStream);

using (FileStream fs = File.Open(Application.dataPath + "/Lesson5_2.tang", FileMode.Open, FileAccess.Read)) {
    // 申明一个 2进制格式化类
    BinaryFormatter bf = new BinaryFormatter();
    // 反序列化
    Person p = bf.Deserialize(fs) as Person;

    fs.Close();
}

(二)反序列化网络二进制数据

byte[] bytes = File.ReadAllBytes(Application.dataPath + "/Lesson5_2.tang");
// 申明内存流对象 一开始就把字节数组传输进去
using (MemoryStream ms = new MemoryStream(bytes)) {
    // 申明一个 2进制格式化类
    BinaryFormatter bf = new BinaryFormatter();
    // 反序列化
    Person p = bf.Deserialize(ms) as Person;

    ms.Close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔗理苦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值