Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB动态注册类类型_Mongodb_C# 4.0 - Fatal编程技术网

MongoDB动态注册类类型

MongoDB动态注册类类型,mongodb,c#-4.0,Mongodb,C# 4.0,在MongoDB中,我需要动态注册类映射,而不需要自己注册所有当前和未来的类 有一个方法叫做 BsonClassMap.RegisterClassMap<T> 有办法吗?我终于找到了解决这个问题的办法。只是为了所有其他有同样问题的人 我检查了mongodb-c#驱动程序源代码,以了解是否有其他方法可以动态注册类型 方法 BsonClassMap.LookupClassMap(type); 将检查提供的类型是否已注册。如果没有,它将注册它并调用AutoMap() 为了确保该类没有

在MongoDB中,我需要动态注册类映射,而不需要自己注册所有当前和未来的类

有一个方法叫做

BsonClassMap.RegisterClassMap<T> 

有办法吗?

我终于找到了解决这个问题的办法。只是为了所有其他有同样问题的人

我检查了mongodb-c#驱动程序源代码,以了解是否有其他方法可以动态注册类型

方法

BsonClassMap.LookupClassMap(type);
将检查提供的类型是否已注册。如果没有,它将注册它并调用AutoMap()

为了确保该类没有在其他地方注册,您应该这样使用它:

if (BsonClassMap.IsClassMapRegistered(type))
  return;

//will check if the type is registered. if not it will be automatically registered.
//AutoMap will also called automatically.
BsonClassMap.LookupClassMap(type);

? 你不必注册任何。Mongo c#驱动程序将处理任何类,并自动序列化和反序列化类的所有公共字段和属性。注册类映射是为了映射某些属性,而不是所有属性。在我的情况下,我需要注册实现接口的所有类(MongoDb驱动程序在处理接口时遇到问题)。这类课程将来可能会增加。为了确保我永远不会忘记一个,我需要自动注册它们。在我的示例中,我可以验证该类是否真正实现了所需的接口。我只想对发布此答案表示感谢,它帮助了我。:-)
if (BsonClassMap.IsClassMapRegistered(type))
  return;

//will check if the type is registered. if not it will be automatically registered.
//AutoMap will also called automatically.
BsonClassMap.LookupClassMap(type);