protobuf-net - 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 及更高版本中是否已弃用?
解决方案
推荐阅读
- sql - SQL MAX() 函数似乎截断了结果
- php - 我们可以在 laravel 的队列中有两个实例“失败”吗?
- python - 使用 VNC 查看器时,模块 screeninfo 无法捕获屏幕分辨率
- html - 如何在 Bootsrap 5 中将行向右移动?
- post - SIM800L GPRS模块与ESP32接口如何自动选择APN
- javascript - 试图将数据表的顶部搜索与 div 的滚动条一起移动
- react-native - 反应原生 Firebase 消息:发布到 Google Play 商店后未收到推送通知
- awk - 使用 awk 替换和添加文本
- c++ - 编译的 ncnn 库未在 Windows 10 代码块 mingw64 上链接
- php - 如何从 php 连接到 PostgreSQL 数据库?