title: How to define the protobuf-net model with a generic base class?
body: <p>Given these types:</p>
  
  <pre><code>  [DataContract]
    public class EntityId
    {
      [DataMember(Order = 1)]
      public string IdAsString { get; set; }
      [DataMember(Order = 2), XmlIgnore]
      public Type Type { get; set; }
  
      #region XML hacks
  
      [XmlElement("Type"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
      public string AssemblyQualifiedTypeName
      {
        get { return ValueConverter.Default.Type2String(Type); }
        set { Type = ValueConverter.Default.String2Type(value); }
      }
  
      #endregion
    }
  
    [DataContract]
    public class EntityRelation
    {
      [DataMember(Order = 1)]
      public int Id { get; set; }
      [DataMember(Order = 2)]
      public string Path { get; set; }
    }
  
    [DataContract]
    public class Base&lt;TId&gt;
    {
      [DataMember(Order = 1)]
      public TId Id { get; set; }
      [DataMember(Order = 2)]
      public string Name { get; set; }
      [DataMember(Order = 3)]
      public EntityId ParentId { get; set; }
      [DataMember(Order = 4)]
      public int LastChanged { get; set; }
      [DataMember(Order = 5)]
      public string Description { get; set; }
      [DataMember(Order = 6)]
      public EntityRelation EntityRelation { get; set; }
    }
  
    [DataContract]
    public class FlowFolder : Base&lt;int&gt;
    {
    }
  </code></pre>
  
  <p>This model definition:</p>
  
  <pre><code>  var m = RuntimeTypeModel.Default;
    m.AutoCompile = false;
    m.Add(typeof(Base&lt;int&gt;), true).AddSubType(1, typeof(FlowFolder));
  </code></pre>
  
  <p>And this usage:</p>
  
  <pre><code>var entity = GetFlowFolder();
  var typeTag = GetTypeTag(entity);
  Model.SerializeWithLengthPrefix(stream, entity, null, PrefixStyle.Base128, typeTag);
  </code></pre>
  
  <p>I get this exception:</p>
  
  <pre><code>System.InvalidOperationException occurred
    Message=Duplicate field-number detected; 1 on: NC.DTO.Base`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
    Source=protobuf-net
    StackTrace:
         at ProtoBuf.Serializers.TypeSerializer..ctor(Type forType, Int32[] fieldNumbers, IProtoSerializer[] serializers, MethodInfo[] baseCtorCallbacks, Boolean isRootType, Boolean useConstructor, CallbackSet callbacks, Type constructType) in z:\Work\protobuf-net-v2\protobuf-net\Serializers\TypeSerializer.cs:line 43
    InnerException: 
  </code></pre>
  
  <p>With the following stack trace:</p>
  
  <pre><code>protobuf-net.dll!ProtoBuf.Serializers.TypeSerializer.TypeSerializer(System.Type forType, int[] fieldNumbers, ProtoBuf.Serializers.IProtoSerializer[] serializers, System.Reflection.MethodInfo[] baseCtorCallbacks, bool isRootType, bool useConstructor, ProtoBuf.Meta.CallbackSet callbacks, System.Type constructType) Line 43   C#
  protobuf-net.dll!ProtoBuf.Meta.MetaType.BuildSerializer() Line 283 + 0xe3 bytes C#
  protobuf-net.dll!ProtoBuf.Meta.MetaType.Serializer.get() Line 209 + 0x11 bytes  C#
  protobuf-net.dll!ProtoBuf.Meta.RuntimeTypeModel.Serialize(int key, object value, ProtoBuf.ProtoWriter dest) Line 357 + 0x51 bytes   C#
  protobuf-net.dll!ProtoBuf.ProtoWriter.WriteObject(object value, int key, ProtoBuf.ProtoWriter writer, ProtoBuf.PrefixStyle style, int fieldNumber) Line 101 + 0x45 bytes    C#
  protobuf-net.dll!ProtoBuf.Meta.TypeModel.SerializeWithLengthPrefix(System.IO.Stream dest, object value, System.Type type, ProtoBuf.PrefixStyle style, int fieldNumber, ProtoBuf.SerializationContext context) Line 467 + 0x24 bytes C#
  protobuf-net.dll!ProtoBuf.Meta.TypeModel.SerializeWithLengthPrefix(System.IO.Stream dest, object value, System.Type type, ProtoBuf.PrefixStyle style, int fieldNumber) Line 435 + 0x32 bytes    C#
  </code></pre>
  
  <p>I am using rev 424.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I do not understand anything. It does not work if I make <code>Base&lt;T&gt;</code> a non generic type as well. I must be missing something really basic here.</p>
  
  <p><strong>EDIT2</strong></p>
  
  <p>Debugging the code reveals that field numbers of the base type properties are collected in the same list with the field numbers of the derived types. From that I deduce that the only way for the inheritance to work is with surrogates. At least in revision 424.</p>