Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-247

Support deserializing to an interface

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.2
    • Affects Version/s: 1.0
    • Component/s: None
    • None

      Suppose you have the following interface and class:

      public interface I {
      int X

      { get; set; }
      }

      public class C : I {
      public ObjectId Id { get; set; }

      public int X

      { get; set; }

      }

      If you insert an instance of C using the interface as the nominal type:

      var c = new C

      { X = 1 }

      ;
      collection.Insert<I>(c);

      The serializer adds a _t discriminator, so the inserted document looks like:

      { "_id" : ObjectId("4df615dce447ad9c44335d73"), "_t" : "C", "X" : 1 }

      but a subsequent attempt to read this document back using the interface as the nominal type fails:

      collection.FindOneAs<I>();

      with the exception: "No serializer found for type TestDeserializeInterface.I.".

      While it is true that deserialization must always be to an instance of a concrete class, in this case the deserializer should be able to figure out which concrete class to use by looking at the discriminator value.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: