简体   繁体   中英

Deserialize a nested struct with MongoDB C# driver

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; }
}

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

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.

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;}

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM