Skip to content

Instantly share code, notes, and snippets.

@aluanhaddad
Created Jan 22, 2018
Embed
What would you like to do?
Bson Serialization/Deserialization
void Main()
{
var bson = new MongoDB.Bson.BsonDocument(new Dictionary<string, object>
{
["ChildishField"] = "childish",
["AnotherChildishField"] = "more childish"
});
var serialized = bson.ToString().Dump();
MongoDB.Bson.Serialization.BsonSerializer.Deserialize<Child1>(serialized).Dump();
}
[BsonIgnoreExtraElements]
[BsonKnownTypes(typeof(Child1))]
public abstract class MyAbstract
{
public string DontCare;
}
[BsonIgnoreExtraElements] // comment this out and it throws as expected
public sealed class Child1 : MyAbstract
{
public string ChildishField;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment