[CSHARP-607] Deserialize Polymorphic Arrays Created: 18/Oct/12  Updated: 20/Mar/14  Resolved: 18/Oct/12

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

Type: Bug Priority: Major - P3
Reporter: Craig Wilson Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The below program will reproduce.

class Program
    {
        static void Main(string[] args)
        {
            var conn = MongoServer.Create();
            var db = conn.GetDatabase("test");
            var coll = db.GetCollection("PersonAddresses");
 
            //coll.RemoveAll();
 
            //// #1 Please insert a bit of data first by uncommenting the lines. THEN comment them out again.
            //GetLastErrorResult err;
            //using (db.RequestStart())
            //{
            //    for (int i = 0; i < 1; i++)
            //    {
            //        coll.Insert<Person>(new Person()
            //        {
            //            Birthday = DateTime.Now,
            //            FirstName = "Silke",
            //            Addresses = new Address[] { 
            //                        new StreetAddress() { City = "Munich" }, 
            //                        new PhoneAddress() { Kind = 'M', Phone = "+49 179 6960101" } }
            //        });
            //    }
            //    err = db.GetLastError();
            //    Console.WriteLine(err);
            //}
 
            foreach (var z in coll.AsQueryable<Person>())
            {
                Console.WriteLine(z);
            }
 
            Console.Read();
        }
    }
 
    public class Person
    {
        public Guid Id { get; set; }
        public DateTime Birthday { get; set; }
        public string FirstName { get; set; }
 
        public IList<Address> Addresses { get; set; }
    }
 
    public abstract class Address
    {
    }
 
    public class PhoneAddress : Address
    {
        public Guid Id { get; set; }
        public string Phone { get; set; }
        public char Kind { get; set; }
    }
 
    public class StreetAddress : Address
    {
        public Guid Id { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string ZIP { get; set; }
        public string Country { get; set; }
    }
 
    public class EMailAddress : Address
    {
        public Guid Id { get; set; }
        public string EMail { get; set; }
    }   



 Comments   
Comment by Robert Stam [ 18/Oct/12 ]

The driver can't deserialize classes it doesn't know about. You have to tell it about the subclasses of your abstract Address class. The easiest way to do that is:

[BsonKnownTypes(typeof(PhoneAddress), typeof(StreetAddress), typeof(EMailAddress))]
public abstract class Address
{
    // ...
}

The reason it works if you do the Insert first is that doing the Insert makes those classes visible to the driver, because they get AutoMapped as part of serializing them.

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