I got an object that has to inherit from a general interface as well as it has a list from that interface as property.
Going to serialize it follows by problems, as the XmlSerialzer is not able to determine the real types that are inside of my MyClass.Items
list elements.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace MyTest
{
public interface IMyInterface
{
string TestProperty { get; set; }
}
public class MyClass : IMyInterface
{
public string TestProperty { get; set; }
public List<IMyInterface> Items { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyClass obj = new MyClass() { TestProperty = "Test" };
XmlSerializer xs = new XmlSerializer(typeof(MyClass)); // <- throws System.NotSupportedException
using (XmlWriter xw = XmlWriter.Create("file.xml", new XmlWriterSettings()
{
Encoding = Encoding.UTF8,
Indent = true,
NewLineHandling = NewLineHandling.Entitize
}))
{
xs.Serialize(xw, obj);
}
}
}
}
How can I serialize a List<T>
where T is an interface?