0
|
1 using System.IO;
|
|
2 using System.Xml.Serialization;
|
|
3
|
|
4 using NUnit.Framework;
|
|
5
|
|
6 using BLToolkit.Reflection;
|
|
7 using BLToolkit.EditableObjects;
|
|
8 using BLToolkit.TypeBuilder;
|
|
9
|
|
10 namespace HowTo.TypeBuilder
|
|
11 {
|
|
12 [TestFixture]
|
|
13 public class XmlSerializationTest
|
|
14 {
|
|
15 /*[a]*/[XmlType(AnonymousType = true)]/*[/a]*/
|
|
16 public abstract class MyClassA
|
|
17 {
|
|
18 public abstract string ValueA { get; set; }
|
|
19 }
|
|
20
|
|
21 [XmlType(AnonymousType = true)]
|
|
22 /*[a]*/[XmlIncludeAbstract(typeof(MyClassA))]/*[/a]*/
|
|
23 /*[a]*/[XmlIncludeAbstract(typeof(MyClassC))]/*[/a]*/
|
|
24 public abstract class MyClassB
|
|
25 {
|
|
26 public abstract string ValueB { get; set; }
|
|
27 public abstract MyClassA ValueMyClassA { get; set; }
|
|
28
|
|
29 public abstract EditableList<MyClassA> MyList { get; set; }
|
|
30 }
|
|
31
|
|
32 /*[a]*/[XmlType("abs:MyClassC")]/*[/a]*/
|
|
33 public abstract class MyClassC : MyClassA { }
|
|
34
|
|
35 [Test]
|
|
36 public void Test()
|
|
37 {
|
|
38 MyClassB original = TypeAccessor<MyClassB>.CreateInstance();
|
|
39 MyClassB serialized;
|
|
40 XmlSerializer sr = new XmlSerializer(/*[a]*/TypeAccessor<MyClassB>.Type/*[/a]*/);
|
|
41
|
|
42 original.ValueB = "string value B";
|
|
43 original.ValueMyClassA.ValueA = "string value A";
|
|
44 original.MyList.Add(TypeAccessor<MyClassA>.CreateInstance());
|
|
45 original.MyList.Add(TypeAccessor<MyClassC>.CreateInstance());
|
|
46
|
|
47 using (MemoryStream stm = new MemoryStream())
|
|
48 {
|
|
49 sr.Serialize(stm, original);
|
|
50 stm.Position = 0L;
|
|
51 serialized = (MyClassB)sr.Deserialize(stm);
|
|
52 }
|
|
53
|
|
54 Assert.That(serialized.ValueB, Is.EqualTo(original.ValueB));
|
|
55 Assert.That(serialized.ValueMyClassA.ValueA, Is.EqualTo(original.ValueMyClassA.ValueA));
|
|
56
|
|
57 Assert.AreEqual(original.MyList.Count, serialized.MyList.Count);
|
|
58 Assert.That(serialized.MyList[0] is MyClassA);
|
|
59 Assert.That(serialized.MyList[1] is MyClassA);
|
|
60 Assert.That(serialized.MyList[1] is MyClassC);
|
|
61
|
|
62 Assert.AreEqual(serialized.MyList[0].GetType(), TypeFactory.GetType(typeof(MyClassA)));
|
|
63 Assert.AreEqual(serialized.MyList[1].GetType(), TypeFactory.GetType(typeof(MyClassC)));
|
|
64 }
|
|
65 }
|
|
66 } |