[CSHARP-247] Support deserializing to an interface Created: 13/Jun/11  Updated: 02/Apr/15  Resolved: 22/Jun/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.0
Fix Version/s: 1.2

Type: Improvement Priority: Minor - P4
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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.



 Comments   
Comment by Robert Stam [ 22/Jun/11 ]

Implemented.

Generated at Wed Feb 07 21:36:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.