I'm trying to run code similar to this:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
[Serializable]
[XmlInclude(typeof(List<Class2>))]
public class Class1
{
private IList<Class2> myArray;
public IList<Class2> MyArray
{
get { return myArray; }
set { myArray = value; }
}
}
public class Class2
{
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}
class Program
{
static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(Class1), new Type[] { typeof(List<Class2>) });
FileStream stream = File.OpenWrite("Data.xml");
ser.Serialize(stream, new List<Class1>());
stream.Close();
}
}
}
Can somebody explain to me what am I doing wrong?
I get a:
Cannot serialize member .. MyArray ... because it is an interface.
Shouldn't the XmlInclude resolve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…