【发布时间】:2010-12-27 13:27:59
【问题描述】:
我正在尝试序列化包含一些 TestChild 对象的 Test 对象数组。
public class Test
{
public string SomeProperty { get; set; }
}
public class TestChild : Test
{
public string SomeOtherProperty { get; set; }
}
class Program
{
static void Main()
{
Test[] testArray = new[]
{
new TestChild { SomeProperty = "test1", SomeOtherProperty = "test2" },
new TestChild { SomeProperty = "test3", SomeOtherProperty = "test4" },
new TestChild { SomeProperty = "test5", SomeOtherProperty = "test6" },
};
XmlSerializer xs = new XmlSerializer(typeof(Test));
using (XmlWriter writer = XmlWriter.Create("test.xml"))
xs.Serialize(writer, testArray);
}
}
我收到 InvalidOperationException 说 TestChild 无法转换为 Test。
这是有道理的,但有没有办法做到这一点?
【问题讨论】:
标签: c# xml serialization xml-serialization