title: MongoDB C# driver type discriminators with generic class inheriting from non-generic base class
body: <p>I'm trying to store a list of objects of a generic class that inherits from a non-generic base class in mongodb using the official C# driver. </p>
  
  <p>My code looks like this:</p>
  
  <pre><code>abstract class MyAbstractClass {}
  
  class MyGenericClass&lt;T&gt;: MyAbstractClass
  {
      [BsonRequired]
      public List&lt;T&gt; Values = new List&lt;T&gt;();
  
      public MyGenericClass(IEnumerable&lt;T&gt; values) 
      {
          Values.AddRange(values);
      }
  }
  
  class MyContainerClass
  {
      [BsonId]
      public string Id;
  
      [BsonRequired]
      public List&lt;MyAbstractClass&gt; ValueCollections = new List&lt;MyAbstractClass&gt;();
  
      public MyContainerClass()
      {
          Id = Guid.NewGuid().ToString();
      }
  }
  </code></pre>
  
  <p>When testing, I create a container object and fill it with instances of the generic class, like so:</p>
  
  <pre><code>var container = new MyContainerClass();
  container.ValueCollections.Add(new MyGenericClass&lt;string&gt;(new[]{"foo","bar","baz"}));
  </code></pre>
  
  <p>When I save this to the DB, the documents added look like this:</p>
  
  <pre><code>{
  "_id": "c5cf5cd1-843f-4d5d-ba8f-5859ae62fd1d",
  "ValueCollections": [
      {
          "_t": "MyGenericClass`1",
          "Values": [
              "foo",
              "bar",
              "baz"
          ]
      }
  ]
  }
  </code></pre>
  
  <p>The type discriminator gets type "MyGenericClass'1" instead of "MyGenericClass'1[System.String]" which means that it can't possibly deserialize this again. </p>
  
  <p>Also, when trying to load these objects from the DB I get an error message: Instances of abstract classes cannot be created. But the type discriminator (if it were correct) should allow the driver to see that it should not created objects of type MyAbstractClass but of MyGenericClass</p>
  
  <p>So my questions are:
  1. Why do I get this error?
  2. Why doesn't it serialize the discriminator correctly?</p>
  
  <p>Thank you for your time.</p>