0

So i have classes:

public class classA : BaseClass
{
    public classA(){}
}


public abstract class BaseClass
{
    public ComplexTypeClass Total { get; set; }
    public List<Item> Items {get; set; } 
    //some other properties
}


public class ComplexTypeClass : ValueObject<ComplexTypeClass>
{
    public ComplexTypeClass()
    {
    }
    public ComplexTypeClass(decimal p1, decimal p2, decimal p)
    {
        P1 = p1;
        P2 = p2;
        P3 = p3;
    }
    public decimal P1 { get; }
    public decimal P2 { get; }
    public decimal P3 { get; }

    //some methods and validations
}


public abstract class ValueObject<T> : IEquatable<T>, IValidatableObject where T : ValueObject<T>
{
    //all implemented interfaces methods
}

And then I try serialize classA, all properties and list are serialized properly, except the ComplexTypeClass property. For this property I get such line in xml <Total />.

All classes have [Serializable], [ComplexType], [XmlType] attributes. I have tried different combinations as well. I have tried using [XmlElement], [XmlRoot] on ComplexTypeClass property. However that did nothing.

I do have another complex type class, which does not have interfaces, and this class is serialized properly.

Does these interfaces has to do anything? Or am i missing something simple and small?

Edit: getting rid of interfaces did not help.

Mantas Čekanauskas
  • 2,218
  • 6
  • 23
  • 43
  • In such cases it's easier to serialize manually that complex type (e.g. as a `string`). For this you mark existing property with `[XmlIgnore]` and create another property (of `string` type), in getter/setter of which you serialize/deserialize value of complex type. This way `XmlSerializer` don't have problems to serialize/deserialize complete type. – Sinatr May 19 '16 at 08:07
  • well, i'll keep that option open, but as I mentioned I have another complex type and it is serialized properly – Mantas Čekanauskas May 19 '16 at 08:09
  • 1
    The problem is that `ComplexTypeClass` doesn't have anything serializable (getter-only properties are [not serialized](http://stackoverflow.com/a/13401220/1997232)). It has nothing to do with base class or interfaces (you would get exception in such case). – Sinatr May 19 '16 at 08:19
  • Yep, that's the case, appreciate for the help ;) – Mantas Čekanauskas May 19 '16 at 08:28
  • @Sinatr you can post solution for future reference if someone else stumbles on such problem ;) – Mantas Čekanauskas May 21 '16 at 15:12
  • I didn't offered you one, only some tips ;) You are the right person to [post the answer](http://stackoverflow.com/help/self-answer) (you have tested code working for exact your case). – Sinatr May 23 '16 at 07:32

1 Answers1

1

If any one stumbles on this sort of problem, as humble @Sinatr pointed out, ComplexTypeClass properties

public decimal P1 { get; }
public decimal P2 { get; }
public decimal P3 { get; }

didn't have set, so XmlSerializer could not repopulate this object

Mantas Čekanauskas
  • 2,218
  • 6
  • 23
  • 43