문제

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.

도움이 되었습니까?

해결책

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 {...}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top