本文介绍了使用 MongoDB C# 驱动程序反序列化嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我毫无问题地序列化了这个 Kontejner 类.

I serialize this Kontejner class without problems.

public struct Dimenzije
{
    public double duzina, sirina, visina;
    public Dimenzije(double d, double s, double v)
    {
        duzina = d; sirina = s; visina = v;
    }
}

public class Kontejner
{

    [BsonId]
    public string Id { get; set; }

    public string Opis { get; set; }
    public Dimenzije Dimenzije { get; set; }

    [BsonElement]
    public double Zapremina
    {
        get
        {
            return Dimenzije.duzina * Dimenzije.sirina * Dimenzije.visina;
        }
    }

    public bool Cvrsti { get; set; }
    public bool Tecni { get; set; }
    public bool Rasuti { get; set; }
}

当我尝试反序列化它时,它无法反序列化 Dimenzije 字段.

When I try to deserialize it, it can't deserialize the Dimenzije field.

我知道这是一个 mapping 问题,但我迫不及待地想快速找到答案,而且我在 MongoDB 文档中没有看到它.提前致谢.

I know this is a mapping issue, but I'm pressured into finding a quick answer, and I don't see it in MongoDB docs. Thanks in advance.

推荐答案

我把 Dimenzije 改成了一个类而不是结构体,像这样:

I changed Dimenzije to be a class instead of struct, like this:

public class Dimenzije
{
    public Dimenzije() { }
    public Dimenzije(double d, double s, double v)
    {
        duzina = d; sirina = s; visina = v;
    }
    public double duzina{get; set;}
    public double sirina{get; set;}
    public double visina{get; set;}

}

它按预期工作.注意 Dimenzije() 构造函数的存在!

It works as expected. Notice the presence of Dimenzije() constructor!

这篇关于使用 MongoDB C# 驱动程序反序列化嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 14:07