问题描述
全部
考虑这个例子:
private class CollectionHolder
{
public ObjectId Id { get; set; }
public MyCollection Collection { get; set; }
}
private class MyCollection : List<int>
{
public MyCollection(List<int> a)
{
this.AddRange(a);
}
}
private static void CollectionTest()
{
var collection = database.GetCollection<MyCollection>("collectionTest");
collection.RemoveAll();
collection.Save(new CollectionHolder { Collection = new MyCollection(new List<int> { 1, 2, 3, 4, 5 }) });
var x = collection.AsQueryable().First(); //exception!
x.ForEach(Console.WriteLine);
}
标记的行抛出异常
反序列化类 MongoDriverTest.Program+CollectionHolder 的 Collection 属性时出错:反序列化类 MongoDriverTest.Program+MyCollection 的容量属性时出错:未将对象引用设置为对象的实例.
我不确定,这是 10gen mongo 驱动程序中的错误,还是无法实现?你怎么看,这应该作为错误发布吗?
I am not sure, is this a bug in 10gen mongo driver, or is this impossible to implement?How do You think, should this be posted as a bug?
此外——避免此类错误的最佳方法是什么?
Moreover -- what is the best way to avoid such kind of errors?
推荐答案
目前,不支持自定义集合.已经在 master 中实现,并将在 1.5 版中存在.在此之前,您无法使用自定义集合来获取您请求的行为.
Currently, custom collections are not supported. There is already implemented in master and will exist in release 1.5 for this. Until then, you can't use custom collections to get the behavior you are requesting.
这篇关于C# 10gen 和 mongo:将成员反序列化为接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!