c# - Protobuf.Net 可以序列化动态字典吗?

标签 c# protobuf-net

我有一本动态字典。

public Dictionary<int, dynamic> Data = new Dictionary<int, dynamic>();

动态部分将包含仅由 5-6 个获取/设置属性组成的简单类,例如类的外观。

public class Class1
{
    public int Property1 { get; set; }
    public int Property2 { get; set; }
}

public class Class2
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

我尝试将这 2 个类添加到我的字典中,然后序列化字典但收到错误。只是想知道 Protobuf.Net 是否能够序列化和反序列化这样的字典?如果是这样,这是如何实现的?

最佳答案

简短的回答是“不是真的”。可能有一些方法可以让它发挥作用,但我不推荐它 - 它很难维护。

protobuf-net(和一般的 protobuf)在序列化器提前知道结构时效果最好。

关于c# - Protobuf.Net 可以序列化动态字典吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15741797/

相关文章:

c# - .net 正则表达式匹配行

c# - 使用 LINQ 覆盖或忽略 C# 中未声明的实体

c# - 在 Pack URI 数据绑定(bind)中转义逗号(快速提问)

c# - 使用 ProtoBuf-net 反序列化派生类型(字典)未正确设置对象字段

c# - 打印由 Microsoft Report Viewer 生成的报告

c# - 无法使用线程写入文件

.net - 如何使用通用基类定义 protobuf-net 模型?

c# - 使用 Protobuf-net 恢复损坏的文件序列化

c# - protobuf-net:反序列化扩展关键字到继承层次结构