在MongoDB中,我需要动态地注册类映射,而不需要自己注册所有当前和未来的类。
有一种方法叫做
BsonClassMap.RegisterClassMap<T> 但是有了这个,我需要知道类(T),不能用Type代替。
我想遍历像这样的程序集类型来注册类映射:
var assemblyTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes()).Where(t => t.IsClass);
foreach (var type in assemblyTypes)
{
//Register classmap for type
}有办法吗?
发布于 2015-01-09 10:25:40
我终于找到了解决这个问题的办法。只是为了其他有同样问题的人。
我检查了mongodb#-驱动程序源代码,以确定是否有另一种方法可以动态注册类型。
该方法
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);https://stackoverflow.com/questions/27858496
复制相似问题