TestChild
개체를 포함하는 Test
개체 배열을 직렬화하려고합니다.서브 클래스로 채워진 기본 클래스의 배열을 XML로 직렬화하는 방법은 무엇입니까?
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);
}
}
TestChild를 Test로 변환 할 수 없다는 InvalidOperationException이 표시됩니다.
이것은 의미가 있지만 어쨌든 그것을 할 수있는 방법이 있습니까?
고마워요! (특히 캐싱에 대한 조언) – neo2862