protobuf-net的preparserializer有什么作用?

问题描述 投票:4回答:1

我假设它查看了您的模型并以某种方式使事情做好了准备,因此您的前几个序列化不会减慢。如果我的消息传递模型的消息传递类带有子类怎么办?将父类放在type参数中是否也准备好所有子级?

c# serialization protobuf-net
1个回答
6
投票

(此答案假定为protobuf-net v2)

如果您指的是Serializer.PrepareSerializer<T>(),那么它肯定会检查所有子类型,因此将为它们准备类型模型(这意味着:它将找出需要序列化的字段/属性等)。它将为parent类预编译(即IL发射)代码,但(针对代码看)不是专门针对派生类型。如果无人看管,派生类型将在首次需要时自行编译。我认为!如果您确实想要,我可以进行彻底检查。

但是,如果您使用RuntimeTypeModel.Default.CompileInPlace(),它将构建整个模型-已知的一切都已准备就绪。当然,这使我们不得不首先告诉模型有关他们的难题; p

我将仔细检查以确保子类型序列化器准备在什么时候使用。级联它们确实确实有意义。


更新:

看起来它确实确实可以级联到[[derived类型,但不能级联到parent类型(如果有):

[Test] public void CheckTypeSpecificCompileInPlaceCascadesToBaseAndChildTypes() { var model = TypeModel.Create(); model[typeof(B)].CompileInPlace(); Assert.IsTrue(model.IsPrepared(typeof(D)), "D"); // sub-sub-type Assert.IsTrue(model.IsPrepared(typeof(C)), "C"); // sub-type Assert.IsTrue(model.IsPrepared(typeof(B)), "B"); // self Assert.IsTrue(model.IsPrepared(typeof(A)), "A"); // base-type } [Test] public void CheckGlobalCompileInPlaceCascadesToBaseAndChildTypes() { var model = TypeModel.Create(); model.Add(typeof (B), true); // give the model a clue! model.CompileInPlace(); Assert.IsTrue(model.IsPrepared(typeof(D)), "D"); // sub-sub-type Assert.IsTrue(model.IsPrepared(typeof(C)), "C"); // sub-type Assert.IsTrue(model.IsPrepared(typeof(B)), "B"); // self Assert.IsTrue(model.IsPrepared(typeof(A)), "A"); // base-type }
[第二次测试通过;第一个测试引用“ A”失败,因此子类型(“ C”和“ D”)已完全编译。由于基本类型仍将按需进行编译,因此可以很好地使用[[可能
,但是如果有用的话,我可以将其写成祖先类型。

([IsPrepared方法仅存在于我的本地副本中]

© www.soinside.com 2019 - 2024. All rights reserved.