c# - 列表不会用 protobuf 序列化

标签 c# protobuf-net

我正在尝试在即将到来的项目中使用 protobuf-net,但我很难弄清楚如何序列化类的列表。我创建了一个 dotnet fiddle 来测试一些基本场景,一切正常,直到我创建一个简单的类并将该类的列表添加到另一个要序列化的类中。我创建类的一个实例并打印它以显示所有值,然后序列化、反序列化并再次打印以显示所有数据已完成该过程,但我的列表始终返回为空。有人知道这是怎么回事吗?

using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections.Generic;
using ProtoBuf;
using ProtoBuf.Meta;

public class Program
{
    public static void Main()
    {

        var item = new MyClass();
        var listItem1 = new ComplexList();
        listItem1.pubField = "first one";
        var listItem2 = new ComplexList();
        listItem2.pubField = "second one";
        item.ComplexList.Add(listItem1);
        item.ComplexList.Add(listItem2);
        item.Print();

        Console.WriteLine();
        Console.WriteLine();

        var serialized = ProtoObjectToByteArray(item);
        var deserialized = ProtoByteArrayToObject<MyClass>(serialized);
        deserialized.Print();
    }

    public static byte[] ProtoObjectToByteArray(object obj)
    {
        if(obj == null)
            return null;
        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.Serialize(ms, obj);
            return ms.ToArray();
        }
    }

    public static T ProtoByteArrayToObject<T>(byte[] arrBytes)
    {
        if(arrBytes == null)
            return default(T);

        using (MemoryStream ms = new MemoryStream(arrBytes))
        {
            return Serializer.Deserialize<T>(ms);
        }
    }
}

[Serializable, ProtoContract]
public class ComplexList {
    public string pubField;

    public ComplexList(){}
}

[Serializable, ProtoContract]
public class MyClass {  
    public List<ComplexList> ComplexList { get; set; }

    public MyClass(){
        ComplexList = new List<ComplexList>();
    }

    public void Print(){
        foreach(var x in ComplexList){
            Console.WriteLine(x.pubField);
        }
    }
}

已按要求更新 https://dotnetfiddle.net/vnfMWh

最佳答案

Protobuf-net 希望您注释您的类型:

[Serializable, ProtoContract]
public class ComplexList {
    [ProtoMember(1)]
    public string pubField;

    public ComplexList(){}
}

[Serializable, ProtoContract]
public class MyClass {
    [ProtoMember(1)]
    public List<ComplexList> ComplexList { get; set; }

    public MyClass(){
        ComplexList = new List<ComplexList>();
    }

    public void Print(){
        foreach(var x in ComplexList){
            Console.WriteLine(x.pubField);
        }
    }
}

protobuf 中的字段被赋予数字标识符,它需要一种可靠的方式来知道哪个字段是哪个数字。

请注意,protobuf-net 不需要 [Serialized]

关于c# - 列表不会用 protobuf 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51959183/

相关文章:

c# - 将 protobuf-net 流转换为 base64 是否会破坏 protobuf 的性能和大小优势?

c# - 将 IQueryable 列表转换为通用列表?

c# - 是否可以在 UHF RFID 标签中写入和读取 ASCII 字符?

c# - 维护多个实例的处理顺序

c# - 使用 Protobuf-net 和 Monotouch for IOS 通过 WCF 序列化 IEnumerable

java - 将 protobuf-net 与 WCF 客户端和 Java 服务器一起使用

c# - System.Drawing.dll 中发生类型为 'System.ArgumentException' 的第一次机会异常

c# - 我可以在 Linq 结果上使用 "Count"属性吗?

c# - 使用相同的模型并行反序列化(高效)

protocol-buffers - Protobuf C# 消息翻译为 JAVA