protobuf.net Unexpected subtype

[亡魂溺海] 提交于 2019-12-10 12:49:24

问题


I'm encountering this Exception in my project using Protobuf.net:

InvalidOperationException "Unexpected sub-type: foo"

I have a class which I'm sending which looks like this:

class message
{
    list<bar> listOfBars;
}

foo inherits off bar, However protobuf seems to choke on this and generate the exception above. Is there some way around this? I need to be able to hold all different subtypes of bar in the list, so a more type constrained solution would be difficult/impossible.


回答1:


I may be mistaken, but I think you need to specify on the inherited class which subtypes inherit from it, for example:

[Serializable, ProtoContract, ProtoInclude(100, typeof(Foo))]
class Bar { }

[Serializable, ProtoContract]
class Foo : Bar { } // Inherits from Bar



回答2:


I'm not 100% on protocol buffers, and maybe I'm way off base here but are you thinking that List is assignable from List where Bar inherits from Foo? This is not the case - they are considered two different types with no relation. In .NET 4, covariant Type parameters are supported but it requires support from the collection (which List does not offer even in .NET 4, as this would be a breaking change - arguably older code that attempts this is broken anyway, but it's still a change in behaviour)



来源:https://stackoverflow.com/questions/3797651/protobuf-net-unexpected-subtype

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