How RuntimeTypeModel can be used to associate ProtoInclude with a type in protobuf-net?

我们两清 提交于 2019-12-09 07:03:53

问题


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.

Is there an example?

Thanks.


回答1:


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;
    }

The above configures the entire type model at runtime, but you can also mix-and-match between automatic (via properties) and explicit (through code).



来源:https://stackoverflow.com/questions/6088004/how-runtimetypemodel-can-be-used-to-associate-protoinclude-with-a-type-in-protob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!