I have a list of many test implements the interface IDoTest, that I want to store in a file. I also want to read from this file.
It seemed natural to simple use the XmlSerializer to store the objects in my List of IDoTest. But when I do this I get a vague I am sorry I cant do that error in the neighborhood of System.Xml.Serialization.TypeDesc.CheckSupported()
Can the XmlSerializer only do trivial jobs? Or am I missing something? They are talking about custom serialization at MSDN. Here is my simplified code example.
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
public interface IDoTest
{
void DoTest();
void Setup();
}
internal class TestDBConnection : IDoTest
{
public string DBName;
public void DoTest()
{
Console.WriteLine("DoHardComplicated Test");
}
public void Setup()
{
Console.WriteLine("SetUpDBTest");
}
}
internal class PingTest : IDoTest
{
public string ServerName;
public void DoTest()
{
Console.WriteLine("MaybeDoAPing");
}
public void Setup()
{
Console.WriteLine("SetupAPingTest");
}
}
internal class Program
{
private static void Main(string[] args)
{
TestDBConnection Do1 = new TestDBConnection { DBName = "SQLDB" };
PingTest Do2 = new PingTest { ServerName = "AccTestServ_5" };
List<IDoTest> allTest = new List<IDoTest> { Do1, (Do2) };
// Now I want to serialize my list.
// Its here where I get the error at allTest
XmlSerializer x = new XmlSerializer(allTest.GetType());
StreamWriter writer = new StreamWriter("mySerializedTestSuite.xml");
x.Serialize(writer, allTest);
}
}
}