gpt4 book ai didi

c# - 为所有类实现 BsonIgnoreExtraElements

转载 作者:IT老高 更新时间:2023-10-28 13:06:46 31 4
gpt4 key购买 nike

我正在将 mongDb 与 MongoDrive 一起使用,我想知道如何在我的所有类中实现 [BsonIgnoreExtraElements]

我知道ConventionProfile有办法,但是不知道怎么实现。

最佳答案

编辑

根据 Evereq 的评论,以下内容已过时。现在使用:

var conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);

使用 SetIgnoreExtraElementsConvention 方法(来自 C# 驱动程序 Serialization TutorialConventions 部分):

var myConventions = new ConventionProfile();
myConventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention()));
BsonClassMap.RegisterConventions(myConventions, (type) => true);

参数(type) => true 是一个取决于类类型的谓词,它决定是否应用约定。因此,根据您的要求,无论如何它都应该简单地返回 true;但如果需要,您可以使用它来设置/排除给定类型的约定。

关于c# - 为所有类实现 BsonIgnoreExtraElements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12944520/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com