1

几周前,我非常成功地使用了 v2 的 protobuf-net 版本。(由于在 Compact Framework 上运行的预编译序列化程序的速度,我想使用 V2。)在我尝试序列化具有 Dictionary 类型属性的对象之前,一切都很好。我收到以下错误:

{“没有为类型定义序列化程序:System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0 .0.0,文化=中性,PublicKeyToken=b77a5c561934e089]]"}

看起来最近为改进字典支持做了一些更改,所以我从今天开始获取最新的代码。在构建 CF3.5 和 FF3.5 dll 后,我再次尝试。现在我收到一个不同的错误“模型一旦冻结就无法更改”。如果我从 Dictionary 属性中删除 ProtoMember 属性,一切似乎都运行良好。

有没有人在 protobuf-net 的 v2 中成功使用字典?这是否还为时过早,无法使用?我的速度测试显示 v2 的速度是原来的两倍,这看起来是否准确(验证了我使用不太稳定的 v2 pre-alpha 位的愿望。)

4

1 回答 1

2

That is of definite interest. Do you have a specific demo that I can use to investigate? (I'm the author)


The problem is simply a bug; during Compile() it should cascade the model the see what other types are needed. It doesn't do this at the moment (but will soon ;p). In regular .NET (with RuntimeTypeModel) this isn't a problem - it can add in the extra type on the fly. But on CF (or with any static-compiled dll) this is not possible.

Until Compile() is fixed, the cheat here is to tell it what else it needs. The easiest way is to touch the indexer for KeyValuePair<string,string>:

model.Add(typeof(Parent), true);
var discard = model[typeof(KeyValuePair<string, string>)];
model.Compile(...);

We don't need the value returned by this indexer (into discard) - we just need it to have executed to fill in some blanks before calling compile.

于 2010-06-17T22:28:38.217 回答