首页 > 解决方案 > Protobuf-net 使用已编译的 TypeModel 时,模型没有可用的类型序列化器

问题描述

嗨,我正在努力更新到新版本的 protobuf-net(2.3.17 -> 3.0.62),但在序列化具有一个包含另一个集合的集合的类的类时遇到了问题(例如:字典<int, Dictionary<int, int>>) 创建和编译 TypeModel 时,TypeModel.Compile() 返回的 TypeModel 将抛出 InvalidOperationException - System.Collections.Generic.Dictionary`2[System.Int32, System.Int32] 可用于模型 CompiledModel... 尝试序列化此字段时。

这以前有效,但似乎已与 3.0 版本中断。

这是我在 linqpad 中写的一个重现问题的示例:

void Main()
{
    RuntimeTypeModel newTypeModel = RuntimeTypeModel.Create();
    newTypeModel.Add(typeof(TestClass), applyDefaultBehaviour: true);
    var compiledModel = newTypeModel.Compile();

    var testObj = new TestClass();
    testObj.Data = new Dictionary<int, Dictionary<int, int>>();

    using (var ms = new MemoryStream())
    {
        compiledModel.Serialize(ms, testObj);
        byte[] data = ms.ToArray();
        data.Dump();
    }
}

[ProtoContract]
public class TestClass : Extensible
{
    [ProtoMember(1)]
    public Dictionary<int, Dictionary<int, int>> Data { get; set; }
}

这种工作流程在 3.0 及更高版本中是否已弃用?

标签: protobuf-net

解决方案


推荐阅读