Say we have a key with values ββthat are polymorphic in their own sense. Consider the following project example:
public class ToBeSerialized { [BsonId] public ObjectId MongoId; public IDictionary<string, BaseType> Dictionary; } public abstract class BaseType { } public class Type1 : BaseType { public string Value1; } public class Type2:BaseType { public string Value1; public string Value2; } internal class Program { public static void Main() { var objectToSave = new ToBeSerialized { MongoId = ObjectId.GenerateNewId(), Dictionary = new Dictionary<string, BaseType> { {"OdEd1", new Type1 {Value1="value1"}}, { "OdEd2", new Type1 {Value1="value1"} } } }; string connectionString = "mongodb://localhost/Serialization"; var mgsb = new MongoUrlBuilder(connectionString); var MongoServer = MongoDB.Driver.MongoServer.Create(mgsb.ToMongoUrl()); var MongoDatabase = MongoServer.GetDatabase(mgsb.DatabaseName); MongoCollection<ToBeSerialized> mongoCollection = MongoDatabase.GetCollection<ToBeSerialized>("Dictionary"); mongoCollection.Save(objectToSave); ToBeSerialized received = mongoCollection.FindOne(); } }
Sometimes, when I try to deserialize it, I get deserialization errors, such as βUnknown discriminator valueβ Specific type name. β What am I doing wrong? If each value stores _t, why can't it display it correctly?
source share