Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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
C# b反序列化<;T>;(bsonDocument)-子对象的Id字段导致异常_C#_Mongodb_Deserialization_Mongodb .net Driver_Bson - Fatal编程技术网

C# b反序列化<;T>;(bsonDocument)-子对象的Id字段导致异常

C# b反序列化<;T>;(bsonDocument)-子对象的Id字段导致异常,c#,mongodb,deserialization,mongodb-.net-driver,bson,C#,Mongodb,Deserialization,Mongodb .net Driver,Bson,我有甲级、乙级和丙级 public class A { public string Id {get;set;} public List<B> Children {get;set;} } public class B { public string Id {get;set;} public string Foo {get;set;} public double Bar {get;set;} } public class C { p

我有甲级、乙级和丙级

public class A  
{ 
    public string Id {get;set;}
    public List<B> Children {get;set;}
}
public class B
{
    public string Id {get;set;}
    public string Foo {get;set;}
    public double Bar {get;set;}
}
public class C 
{
    public string Id {get;set;}
    //This property will hold a serialized version of Class A.
    //The scenario requirement is that it can hold any arbitrary BsonDocument of different types
    public BsonDocument Properties {get;set;} 
}

var instanceOfClassC = collection.Find(...).First();

var data = BsonSerializer.Deserialize<A>(instanceOfClassC.Properties);
公共A类
{ 
公共字符串Id{get;set;}
公共列表子项{get;set;}
}
公共B级
{
公共字符串Id{get;set;}
公共字符串Foo{get;set;}
公共双栏{get;set;}
}
公共C类
{
公共字符串Id{get;set;}
//此属性将保存类a的序列化版本。
//场景需求是,它可以保存任何不同类型的任意BsonDocument
公共BsonDocument属性{get;set;}
}
var instanceOfClassC=collection.Find(…).First();
var data=BsonSerializer.Deserialize(instanceOfClassC.Properties);
最后一行导致下面的异常。 如果我将ignore BsonElement添加到类B的Id属性中,它就可以正常工作。 但我需要那个Id属性

例外情况:

MongoDB.Bson.dll中发生类型为“System.FormatException”的未处理异常

其他信息:反序列化类命名空间的子属性时出错。A:元素“Id”与类命名空间的任何字段或属性都不匹配。B

MongoDB.Bson.dll中发生类型为“System.FormatException”的未处理异常

问题似乎在于,属性B.Id实际上在MongoDb中存储为“Id”,因为它在存储之前被“序列化”到BsonDocument。同样的模式在其他方面总是完美的,但是MongoDb将在写入时转换Id=>\u Id

要求:C类。属性包含任意类型的其他有效类类型,不能在类声明中更改为类型A。它工作平稳-除了嵌套的Id属性

更新:发现了一个残酷的黑客解决方案:将BsonDocument中的所有“Id”属性重命名为“\u Id”,然后再将发送到MongoDb。然后反序列化按预期工作。我使用json的字符串替换来实现这一点 json.Replace(“\”Id\”,“\”u Id\”)


有谁有更好的解决方案吗?

更清洁的解决方案是:

public class A  
{ 
    public string Id {get;set;}
    public List<B> Children {get;set;}
}
[BsonNoId] // this solves the problem
public class B
{
    public string Id {get;set;}
    public string Foo {get;set;}
    public double Bar {get;set;}
}
公共A类
{ 
公共字符串Id{get;set;}
公共列表子项{get;set;}
}
[BsonNoId]//这就解决了问题
公共B级
{
公共字符串Id{get;set;}
公共字符串Foo{get;set;}
公共双栏{get;set;}
}
您也可以使用BsonClassMap在没有属性的情况下执行此操作:

BsonClassMap.RegisterClassMap<B>(cm =>
{
    cm.SetIdMember(null);
});
BsonClassMap.RegisterClassMap(cm=>
{
cm.SetIdMember(空);
});
如果要映射的类型来自不同的库,则这是有用的