集合
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System; using System.Collections.Generic; using System.Xml.Serialization; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var f = new XmlSerializer(typeof(TestClass)); using (var sw = new StringWriter()) { f.Serialize(sw, new TestClass()); using (var sr = new StringReader(sw.ToString())) { var bt = (TestClass) f.Deserialize(sr); Console.WriteLine(bt.ListIdentical()); } } } } public class TestClass { private List<int> PrivateList; public List<int> MyList; public bool ListIdentical() { return PrivateList == MyList; } public TestClass() { PrivateList = new List<int>() { 123, 567 }; MyList = PrivateList; } } } |
运行结果为 True 。实际上,如果把 MyList 换成只读属性,只要其值非 null / Nothing,则程序仍可以正常运行。
也就是说,反序列化过程复用了已有的集合实例。 继续阅读“XmlSerializer 测试 2”
