实现所有类BsonIgnoreExtraElements

编程入门 行业动态 更新时间:2024-10-28 05:15:06
本文介绍了实现所有类BsonIgnoreExtraElements的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

,我不知道如何可以实现我所有的班级在 [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

本文发布于:2023-11-15 22:04:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BsonIgnoreExtraElements

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!