如何RuntimeTypeModel可以用来ProtoInclude与protobuf网一个类型关联?可以用来、类型、RuntimeTypeModel、protobuf

由网友(阳光总在风雨后)分享简介:正如我理解,RuntimeTypeModel允许ProtoInclude与类型,当类型声明不能改变这对于箱子有用关联。但我很难理解它是如何实际完成的。As I understood, RuntimeTypeModel allows to associate ProtoInclude with a type, whic...

正如我理解,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).

阅读全文

相关推荐

最新文章