title: Serialize inherited classes using protobuf-net
body: <p>I have a problem serializing derived classes using protobuf-net. I don't know if it is because it is not supported, or I am doing something wrong.</p>
  
  <p>I have a generic base class (which I can serialize directly) and then i make a specialization of this, but this one I can't serialize. The following is the code for the two classes and a example of usage. Am i doing something wrong?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Added restriction to the generic types</p>
  
  <p><strong>Base</strong></p>
  
  <pre><code>[ProtoBuf.ProtoContract]
  public class Base&lt;TKey, TValue&gt;
      where TKey : Key
      where TValue : Key
  {
      [ProtoBuf.ProtoMember(1)]
      public Dictionary&lt;TKey, Dictionary&lt;TKey, TValue&gt;&gt; Data { get; set; }
      [ProtoBuf.ProtoMember(2)]
      public TValue DefaultValue { get; set; }
  
      public Base()
      {
          this.Data = new Dictionary&lt;TKey, Dictionary&lt;TKey, TValue&gt;&gt;();
      }
  
      public Base(TValue defaultValue)
          : this()
      {
          this.DefaultValue = defaultValue;
      }
  
      public TValue this[TKey x, TKey y]
      {
          get
          {
              try { return this.Data[x][y]; }
              catch { return this.DefaultValue; }
          }
          set
          {
              if (!this.Data.ContainsKey(x))
                  this.Data.Add(x, new Dictionary&lt;TKey, TValue&gt; { { y, value } });
              else
                  this.Data[x][y] = value;
          }
      }
  }
  </code></pre>
  
  <p><strong>Key class</strong></p>
  
  <pre><code>public abstract class Key
  {
  }
  </code></pre>
  
  <p><strong>Specialized key</strong></p>
  
  <pre><code>[ProtoBuf.ProtoContract]
  public class IntKey : Key
  {
      [ProtoBuf.ProtoMember(1)]
      public int Value { get; set; }
  
      public IntKey() { }
  
      public override int GetHashCode()
      {
          return this.Value.GetHashCode();
      }
  
      public override bool Equals(object obj)
      {
          if (ReferenceEquals(obj, null)) return false;
          if (ReferenceEquals(obj, this)) return true;
  
          var s = obj as IntKey;
          return this.Value.Equals(s.Value);
      }
  }
  </code></pre>
  
  <p><strong>Specialized class</strong></p>
  
  <pre><code>[ProtoBuf.ProtoContract]
  public class IntWrapper&lt;TKey&gt; : Base&lt;TKey, IntKey&gt;
      where TKey : Key
  {
      public IntWrapper()
          : base() { }
  
      public IntWrapper(IntKey defaultValue)
          : base(defaultValue) { }
  }
  </code></pre>
  
  <p><strong>An example of usage</strong></p>
  
  <pre><code>var path = @"C:\Temp\data.dat";
  var data = new IntWrapper&lt;IntKey&gt;(new IntKey { Value = 0 }); // This will not be serialized
  for(var x = 0; x &lt; 10; x++)
  {
      for(var y = 0; y &lt; 10; y++)
          data[new IntKey { Value = x }, new IntKey { Value = y }] = new IntKey { Value = x + y };
  }
  
  using (var fileStream = new FileStream(path, FileMode.Create))
  {
      ProtoBuf.Serializer.Serialize(fileStream, data);
  }
  </code></pre>