Skip to content

Instantly share code, notes, and snippets.

@aluanhaddad
Created January 22, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aluanhaddad/a27b622170234b2359e2cf61e138e91d to your computer and use it in GitHub Desktop.
Save aluanhaddad/a27b622170234b2359e2cf61e138e91d to your computer and use it in GitHub Desktop.
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