1

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.

displayName
  • 13,888
  • 8
  • 60
  • 75
irfanka
  • 129
  • 1
  • 14

1 Answers1

1

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!

irfanka
  • 129
  • 1
  • 14
  • Hope the pressure is released. Congrats. :) – displayName Jun 03 '14 at 21:03
  • Also, you don't need that constructor. If your constructor does nothing then you don't even need to write it. An empty constructor is created by default in C#. – displayName Jun 03 '14 at 21:03
  • Yeah, I know. The `Dimenzije(double d, double s, double v)` does something else. Cheers, mate :) – irfanka Jun 03 '14 at 21:11
  • 2
    This does absolutely not reply to original question on how to deserialize a struct with MongoDB driver. Changing a struct into a class is changing the problem, not finding a solution. – Jamby Apr 14 '17 at 08:49