Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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
如何使用mongo C#驱动程序反序列化派生类数组的特定属性。无法创建抽象类的实例_C#_Mongodb_Serialization - Fatal编程技术网

如何使用mongo C#驱动程序反序列化派生类数组的特定属性。无法创建抽象类的实例

如何使用mongo C#驱动程序反序列化派生类数组的特定属性。无法创建抽象类的实例,c#,mongodb,serialization,C#,Mongodb,Serialization,我使用的mongo C#驱动程序版本是1.1。我的代码结构如下所示 public abstract Class BaseClass { public int BCProp {get; set;} } public class DerivedClass1 : BaseClass { public int DCProp1 {get; set;} } public class DerivedClass2 : BaseClass { public int DCProp2 {g

我使用的mongo C#驱动程序版本是1.1。我的代码结构如下所示

public abstract Class BaseClass
{
    public int BCProp {get; set;}
}

public class DerivedClass1 : BaseClass
{
    public int DCProp1 {get; set;}
}

public class DerivedClass2 : BaseClass
{
    public int DCProp2 {get; set;}
}

public class ClassOfInterest
{
    public int Prop1 {get; set;}

    // I want to bring back only certain values
    // from the elements in this array while deserializing
    public BaseClass[] ElementArray {get; set;}
}
在将文档插入MongoDB之前,我使用BsonClassMap注册这些类,并将鉴别器设置为具有名称空间的类名。因此,当我创建一个ClassOfInterest对象,并且ElementArray是一个DerivedClass1元素类型的数组时,当我将其插入DB时,数组元素的“\u t”作为“DerivedClass1”。根据有关的文档,所有这些看起来都不错

出于某种原因,我决定只反序列化ClassOfInterest的一些属性。我不想反序列化Prop1,我只想要ElementArray,所以我编写了如下代码

// Here I am specifying that I am interested only in ElementArray
// A Element in ElementArray will be of type DerivedClass1 and will
// include both BCProp and DCProp1
FieldsBuilder _fb = new FieldsBuilder();
_fb.Include("ElementArray");
List<string> IncludedFields = new List<string>();
var dic = _fb.ToBsonDocument().ToDictionary();
IncludedFields.AddRange(dic.Keys.ToList());

// I am querying DB
MongoCollection<ClassOfInterest> mcoll = ActiveDb.GetCollection<ClassOfInterest>(COICollName);
List<ClassOfInterest> COIObjects = mcoll.FindAll().SetFields(IncludedFields.ToArray()).ToList();
//这里我指定我只对ElementArray感兴趣
//ElementArray中的元素将是DerivedClass1类型,并且将
//包括BCProp和DCProp1
FieldsBuilder_fb=新的FieldsBuilder();
_fb.包括(“元素数组”);
List IncludedFields=新列表();
var dic=_fb.ToBsonDocument().ToDictionary();
IncludedFields.AddRange(dic.Keys.ToList());
//我正在查询数据库
MongoCollection mcoll=ActiveDb.GetCollection(COICollName);
List COIObjects=mcoll.FindAll().SetFields(IncludedFields.ToArray()).ToList();
以上方法很好。返回的对象只有ElementArray,不包括Prop1。鉴别器工作,返回的对象在ElementArray中具有DerivedClass1类型的元素

同样,出于某种原因,我不想反序列化来自DerivedClass1的所有内容。所以我做了下面的工作

// Notice that I want to get back only BCProp in all ElementArray
FieldsBuilder _fb = new FieldsBuilder();
_fb.Include("ElementArray.BCProp");
List<string> IncludedFields = new List<string>();
var dic = _fb.ToBsonDocument().ToDictionary();
IncludedFields.AddRange(dic.Keys.ToList());

// I am querying DB
MongoCollection<ClassOfInterest> mcoll = ActiveDb.GetCollection<ClassOfInterest>(COICollName);
List<ClassOfInterest> COIObjects = mcoll.FindAll().SetFields(IncludedFields.ToArray()).ToList();
//请注意,我只想返回all ElementArray中的BCProp
FieldsBuilder_fb=新的FieldsBuilder();
_fb.Include(“ElementArray.BCProp”);
List IncludedFields=新列表();
var dic=_fb.ToBsonDocument().ToDictionary();
IncludedFields.AddRange(dic.Keys.ToList());
//我正在查询数据库
MongoCollection mcoll=ActiveDb.GetCollection(COICollName);
List COIObjects=mcoll.FindAll().SetFields(IncludedFields.ToArray()).ToList();
然而,这一次,我得到了错误“无法创建抽象类的实例”


这次出了什么问题?如果我要求整个ElementArray,它会将Element Array中的元素正确地反序列化为DerivedClass1。但是,当我请求特定属性(属于基类)时,我得到了错误

我没有要求使用鉴别器,所以反序列化程序不知道如何处理它,并试图创建抽象类的对象。 而不是仅仅做

_fb.Include("ElementArray.BCProp")
是的

现在可以了

_fb.Include("ElementArray.BCProp"); 
_fb.Include("ElementArray._t");