{"id":4833,"date":"2014-03-30T15:49:17","date_gmt":"2014-03-30T15:49:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/protobuf-net-not-specify-sub-types-of-a-base-class-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:49:17","modified_gmt":"2014-03-30T15:49:17","slug":"protobuf-net-not-specify-sub-types-of-a-base-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/protobuf-net-not-specify-sub-types-of-a-base-class-collection-of-common-programming-errors\/","title":{"rendered":"protobuf-net &#8211; Not specify sub types of a base class-Collection of common programming errors"},"content":{"rendered":"<p>I have an inheritance chain such as this:<\/p>\n<pre><code>[ProtoContract]\npublic abstract class Message\n{\n    [ProtoMember(1, OverwriteList = true)]\n    public List Headers {get; set;}\n}\n\n[ProtoContract]\npublic class EventMessage : Message\n{\n    [ProtoMember(2)]\n    public T Event {get; set;}\n}\n<\/code><\/pre>\n<p>The inheritance chain is very straight forward (my . In order to get the Headers to be included in the serialization, I need to do:<\/p>\n<pre><code>RuntimeTypeModel.Default[typeof(Packet)].AddSubType(3, typeof(Message));\n<\/code><\/pre>\n<p>I know this sort of answer (the line above) has been documented on quite a few StackOverflow posts. However, I don&#8217;t like this design because then I need to declare all my subtypes ahead of time and it&#8217;s also implying a limited, small number of subtypes.<\/p>\n<p>I&#8217;m attempting to code a message bus in my application, using protobuf-net for the serialization\/deserialization. The message bus needs to send out &#8216;events&#8217; and respond to request\/replies. Since I have many (easily &gt; 100) events in my system, I don&#8217;t want to declare a subtype for every closed generic type in the RuntimeTypeModel.<\/p>\n<p>Does protobuf-net have the ability to infer subtypes\/classes? Or ideally, I&#8217;d like something like:<\/p>\n<pre><code>RuntimeTypeModel.Default[typeof(Packet)].AddSubType(3, typeof(Message));\n<\/code><\/pre>\n<p>(Which I tried and it doesn&#8217;t work).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have an inheritance chain such as this: [ProtoContract] public abstract class Message { [ProtoMember(1, OverwriteList = true)] public List Headers {get; set;} } [ProtoContract] public class EventMessage : Message { [ProtoMember(2)] public T Event {get; set;} } The inheritance chain is very straight forward (my . In order to get the Headers to be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4833","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4833"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4833\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}