,我不知道如何可以实现我所有的班级在 [BsonIgnoreExtraElements]
I'm using mongDb with MongoDrive, I wonder how I can implement to all my classes the [BsonIgnoreExtraElements].
我知道还有就是通过 ConventionProfile 的一种方式,但我不知道如何实现它。
I know there is a way through the ConventionProfile, but I do not know how to implement it.
推荐答案使用 SetIgnoreExtraElementsConvention 方法(从会展的C#的驱动程序部分的连载教程):
Use the SetIgnoreExtraElementsConvention method (from the Conventions section of the C# Driver Serialization Tutorial):
var myConventions = new ConventionProfile(); myConventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention())); BsonClassMap.RegisterConventions(myConventions, (type) => true);参数(类型)=>真正是依赖于类的类型,确定是否适用公约的谓语。所以,按您的要求它应该简单地返回true不管;但你可以使用它来设置/排除公约上给出类型,如果你想要的。
The parameter (type) => true is a predicate depending on the class type, that determines whether to apply the convention. So per your requirement it should simply return true regardless; but you could use this to set/exclude the convention on given types if you wanted.
修改
每Evereq的评论,上面已经过时了。现在用途:
Per Evereq's comment, the above is obsolete. Now use:
var conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) }; ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);更多推荐
实现所有类BsonIgnoreExtraElements
发布评论