Since XmlSerializer can not serialize any other properties when the class is inherited from List <>
, I try to solve them with the DataContractSerializer
. This should work, as described here: When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes
But I get the same results. If the object is inherited from List <>
the TestValue
property is not serialized.
using System.Runtime.Serialization;
[Serializable]
public class XSerBase
{
[DataMember]
public XSerTest XSerTest { get; set; } = new XSerTest();
}
[Serializable]
public class XSerTest : List<string>
{
[DataMember]
public string TestValue { get; set; }
}
{// my serialize / deserialize example
XSerBase objectSource = new XSerBase();
objectSource.XSerTest.TestValue = "QWERT";
MemoryStream mem = new MemoryStream();
DataContractSerializer dcsSource = new DataContractSerializer(typeof(XSerBase));
dcsSource.WriteObject(mem, objectSource);
mem.Position = 0;
XSerBase objectDestination = null;
DataContractSerializer dcsDestination = new DataContractSerializer(typeof(XSerBase));
objectDestination = (dcsDestination.ReadObject(mem) as XSerBase);
// objectDestination.XSerTest.TestValue is null
// objectDestination.XSerTest.TestValue is "QWERT", when XSerTest is not inherited from List<string>
}
Am I missing an attribute?