Is there a flat that will tell the C# Mongo BsonSerializer to ignore elements that do not exist in the poco class
Example collection
Animal {"Type" : "Cat", "Skill" : "Jump"}
Animal {"Type" : "Dog", "Skill" : "Bark", "Owner" : "Jimmy"}
If the cat C# class only has
public string Type {get;set;}
public string Skill {get;set;}
When I attempt to execute the following
var test = BsonSerializer.Deserialize<Animal>(result);
The first item will work fine, the second one will throw an exception that Owner does not exist.
Use [BsonIgnoreExtraElements] attribute on Cat class.
From attribute summary:
Specifies whether extra elements should be ignored when this class is deserialized.
也许您可以将其反序列化为一个对象,并使用动态来接收它。
dynamic test = BsonSerializer.Deserialize<object>(result);
Or you can use Convention to do it for all types at once
var conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, _ => true);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.