Question

I have MongoCollection<myObject>.

Some of the "myObject" in the collection contains another object (lets say "myObject2") and some of them contains an object that inherited from "myObject2" ("myInheritedObjet")

Now I want to deserialize the Items from the collection, but I'm getting an error because "myObject2" don't have some of the attributes of "myInheritedObjet".

An error occurred while deserializing the "myobject" property: Element 'Name' does not match any field or property of class "myobject"

For example:

MongoCollection<Person>

Person
    string name;
    Animal[] pets;

Animal
    string AType;

Dog : Animal
    string Name;

If Person have a Dog, this person can't deserialize because the Animal Type dosn't have "Name".

How can I fix it? Thanks.

Was it helpful?

Solution

Ok I found this: deserializing polymorphic classes

All I needed to do is mapping the base class to the subclass with:

[BsonKnownTypes(typeof(subclass)]
class BaseClass {...}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top