title: Inheritance in Protobuf-net: ProtoInclude and compatibility
body: <p>As described in <a href="https://stackoverflow.com/questions/947666/what-does-the-protoinclude-attribute-mean-in-protobuf-net">this post</a>, we can use <strong>ProtoInclude</strong> attribute to manage class hierarchy serialization. And if we use just protobuf-net, it works quite fine both directions. But problem occurs when we try to deserialize messages, serialized by "external" legacy Protocol Buffers implementations, ex. Java, etc. </p>
  
  <p>As mentioned in that post above, Protobuf-net recognizes class hierarchy by "reversed" bytes sequences, when child class fields serialized before parent's. But legacy code serialize them in "proper" order and protobuf-net throws an <em>"Unable to cast object of type 'A' to type 'B'."</em> exception during deserialization. In opposite direction it works fine, legacy code can deserialize "hierarchic" messages produced by protobuf-net library.   </p>
  
  <p>I can not affect byte serialization order on opposite side of the pipe. How can I correctly deserialize this type of messages on .NET protobuf-net side?     </p>
  
  <p><strong>Update: code examples</strong></p>
  
  <p>On our end of the line we have original protobuf-net hierarchic classes:</p>
  
  <pre><code>[ProtoContract, ProtoInclude(10, typeof(B))]
  public class A
  {
      [ProtoMember(1)]
      public int Age;
  }
  
  public class B : A
  {
      [ProtoMember(2)]
      public int Balls;
  }
  </code></pre>
  
  <p>On the other end of the line classes are generated using .proto file:</p>
  
  <pre><code>message B {
      optional int32 balls = 2;   
  }
  
  message A {
      optional int32 age = 1; 
      optional B b = 10;
  }
  </code></pre>
  
  <p>Generated classes example, we could use protobuf-net generator to create them for .NET:</p>
  
  <pre><code>[ProtoContract]
  public class A_generated
  {
      [ProtoMember(1)]
      public int Age;
  
      [ProtoMember(10)]
      public B b;
  }
  
  [ProtoContract]
  public class B_generated 
  {
      [ProtoMember(2)]
      public int Balls;
  }
  </code></pre>
  
  <p>So now, let's serialize and deserialize class B:</p>
  
  <ul>
  <li>Serialize and deserialize back <em>original</em> classes - OK</li>
  <li>Serialize and deserialize back <em>generated</em> classes - OK</li>
  <li>Serialize <em>original</em> and deserialize as <em>generated</em> - OK</li>
  <li>Serialize <em>generated</em> and deserialize as <em>original</em> - <strong>FAIL</strong>, <em>"Unable to cast object of type 'A' to type 'B'."</em> exception</li>
  </ul>
  
  <p>I've investigated resulting bytes and discovered a difference - <strong>bytes order</strong>. </p>
  
  <p>Example: let Age=10 and Balls=23. Then:</p>
  
  <ul>
  <li><em>original</em> B serialized: [82, 2, 16, 23, 8, 10], could be deserialized using both as <em>original</em> as <em>generated</em> classes;</li>
  <li><em>generated</em> B serialized: [8, 10, 82, 2, 16, 23], can NOT be deserialized using protobuf-net <em>original</em> classes above. </li>
  </ul>
  
  <p>I hope now it's clear enough, and wish to get positive answer: yes, there is a way to use ProtoInclude and deserialize generic classes. </p>