将对象的字节数组转换为对象的列表

本文关键字:对象 列表 转换 字节数 字节 数组 | 更新日期: 2023-09-27 18:07:32

我有一个对象/类,不同的值作为成员,所有可序列化,我可以转换一个对象到一个字节[]和其他方式。我把这些对象的列表转换成一个大字节数组,我怎么能把它转换回来呢?

示例对象:

using System;
using System.IO;
[Serializable]
public class MyItem {
    internal string m_name = "";
    internal int m_position = 0;
    internal float m_color = 0f;
    internal int m_direction = 0;
    internal float m_power = 0f;
    public MyItem(string name, int position, float color, int direction, float power) {
        m_name = name;
        m_position = position;
        m_color = color;
        m_direction = direction;
        m_power = power;
    }
    public byte[] Serialize() {
        using (MemoryStream m = new MemoryStream()) {
            using (BinaryWriter writer = new BinaryWriter(m)) {
                writer.Write(m_name);
                writer.Write(m_position);
                writer.Write(m_color);
                writer.Write(m_direction);
                writer.Write(m_power);
            }
            return m.ToArray();
        }
    }
    public static MyItem Desserialize(byte[] data) {
        string name;
        int position;
        float color;
        int direction;
        float power;
        using (MemoryStream m = new MemoryStream(data)) {
            using (BinaryReader reader = new BinaryReader(m)) {
                name = reader.ReadString();
                position = reader.ReadInt32();
                color = reader.ReadSingle();
                direction = reader.ReadInt32();
                power = reader.ReadSingle();
            }
        }
        return new MyItem(name, position, color, direction, power);
    }
}

和转换成字节数组:

List<MyItem> itemlist = <...>;
List<byte[]> byteList = new List<byte[]>();
for (int i = 0; i < itemlist.Count; i++) {
    byteList.Add(itemlist[i].Serialize());
}
byte[] data = byteList.SelectMany(bytes => bytes).ToArray();

转换:

????????

将对象的字节数组转换为对象的列表

如注释中所述,您需要写入流中每个对象的字节,以便能够反序列化单个对象。例子:

public static class MyItemSerializer
{
    public static byte[] Serialize(this IEnumerable<MyItem> items)
    {
        using (MemoryStream m = new MemoryStream()) 
        {
            using (BinaryWriter writer = new BinaryWriter(m, System.Text.Encoding.UTF8, true)) 
            {
                foreach (var item in items) 
                {
                    var itemBytes = item.Serialize();
                    writer.Write(itemBytes.Length);
                    writer.Write(itemBytes);
                }
            }
            return m.ToArray();
        }
    }
    public static List<MyItem> Deserialize(byte[] data)
    {
        var ret = new List<MyItem>();
        using (MemoryStream m = new MemoryStream(data))
        {
            using (BinaryReader reader = new BinaryReader(m, System.Text.Encoding.UTF8)) 
            {
                while (m.Position < m.Length)
                {
                    var itemLength = reader.ReadInt32();
                    var itemBytes = reader.ReadBytes(itemLength);
                    var item = MyItem.Desserialize(itemBytes);
                    ret.Add(item);
                }
            }
        }
        return ret;
    }
}

在这里你可以看到它的作用:https://dotnetfiddle.net/Nk2cks

但是。net已经包含了一个叫做BinaryFormatter的序列化器

Protobuf by google是另一种可能性