本文介绍了如何RuntimeTypeModel可以用来ProtoInclude与protobuf网一个类型关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如我理解,RuntimeTypeModel允许ProtoInclude与类型,当类型声明不能改变这对于箱子有用关联。但我很难理解它是如何实际完成的。
As I understood, RuntimeTypeModel allows to associate ProtoInclude with a type, which is useful for cases when the type declaration cannot be changed. But I find it hard to understand how it is actually done.
有一个例子吗?
感谢。
推荐答案
AddSubType()
用于指定派生类型,以及他们的标识;例如(满code ):
AddSubType()
is used to specify derived types, along with their identifier; for example (full code):
static RuntimeTypeModel CreateModel() {
var model = TypeModel.Create();
model[typeof(NotInvolved)].Add(1, "D");
model[typeof(SomeBase)]
.Add(1, "A")
.AddSubType(2, typeof(SomeDerived))
.AddSubType(3, typeof(AnotherDerived));
model[typeof(SomeDerived)].Add(1, "B");
model[typeof(AnotherDerived)].Add(1, "C");
model[typeof(AlsoNotInvolved)].Add(1, "E");
return model;
}
上面的配置在运行时整个类型的模型,但你也可以混合和匹配的自动(通过性)和明确的(通过code)。
The above configures the entire type model at runtime, but you can also mix-and-match between automatic (via properties) and explicit (through code).
这篇关于如何RuntimeTypeModel可以用来ProtoInclude与protobuf网一个类型关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!