I wonder if there is any way to serialize to xml a class that contains list of generics?
It looks like this:
class Program
{
static void Main(string[] args)
{
var o = new ContainerClass();
o.Values = new List<SomeClass> {new SomeClass<int>(), new SomeClass<long>()};
XmlSerializer xs = new XmlSerializer(typeof(ContainerClass));
MemoryStream buffer = new MemoryStream();
using (TextWriter writer = new StreamWriter(buffer))
{
xs.Serialize(writer, o); // InvalidOperationException here
}
var xml = Encoding.UTF8.GetString(buffer.ToArray());
}
}
public class ContainerClass
{
public List<SomeClass> Values { get; set; }
}
public class SomeClass
{
}
public class SomeClass<T> : SomeClass
{
}
I know that there is a way to pass extra types to the serializer, but there is no way to now every combination that could probably appear.
I’ve tried to implement IXmlSerializable i various ways without success.
Any idea how to deal with it?
I am also open for external libraries if there is any that can handle it.
I’ve found YAXLib which does the job.