corrade-vassal – Blame information for rev 16

Subversion Repositories:
Rev:
Rev Author Line No. Line
13 eva 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
16 eva 7 using System;
13 eva 8 using System.Collections.Generic;
16 eva 9 using System.IO;
13 eva 10 using System.Xml;
11 using System.Xml.Schema;
12 using System.Xml.Serialization;
13  
14 namespace wasSharp
15 {
16 eva 16 public static class Collections
13 eva 17 {
18 /// <summary>
19 /// A serializable dictionary class.
20 /// </summary>
21 /// <typeparam name="TKey">the key</typeparam>
22 /// <typeparam name="TValue">the value</typeparam>
23 [XmlRoot("Dictionary")]
24 public class SerializableDictionary<TKey, TValue>
25 : Dictionary<TKey, TValue>, IXmlSerializable
26 {
27 #region IXmlSerializable Members
28  
16 eva 29 public SerializableDictionary(IEnumerable<KeyValuePair<TKey, TValue>> kvp)
30 {
31 foreach (var i in kvp)
32 {
33 Add(i.Key, i.Value);
34 }
35 }
36  
37 public SerializableDictionary()
38 {
39 }
40  
41 /// <summary>
42 /// Deep-clones the serializable dictionary.
43 /// </summary>
44 /// <returns>a deep clone of the original dictionary</returns>
45 public SerializableDictionary<TKey, TValue> Clone()
46 {
47 SerializableDictionary<TKey, TValue> clone;
48 try
49 {
50 using (var writer = new MemoryStream())
51 {
52 var serializer =
53 new XmlSerializer(
54 typeof (SerializableDictionary<TKey, TValue>));
55 serializer.Serialize(writer, this);
56 writer.Seek(0, SeekOrigin.Begin);
57 clone = (SerializableDictionary<TKey, TValue>)
58 new XmlSerializer(
59 typeof (SerializableDictionary<TKey, TValue>))
60 .Deserialize(writer);
61 }
62 }
63 /* cloning failed so return an empty dictionary */
64 catch (Exception)
65 {
66 clone = new SerializableDictionary<TKey, TValue>();
67 }
68 return clone;
69 }
70  
13 eva 71 public XmlSchema GetSchema()
72 {
73 return null;
74 }
75  
76 public void ReadXml(XmlReader reader)
77 {
16 eva 78 var keySerializer = new XmlSerializer(typeof (TKey));
79 var valueSerializer = new XmlSerializer(typeof (TValue));
13 eva 80  
16 eva 81 var wasEmpty = reader.IsEmptyElement;
13 eva 82 reader.Read();
83  
84 if (wasEmpty)
85 return;
86  
16 eva 87 while (!reader.NodeType.Equals(XmlNodeType.EndElement))
13 eva 88 {
89 reader.ReadStartElement("Item");
90  
91 reader.ReadStartElement("Key");
16 eva 92 var key = (TKey) keySerializer.Deserialize(reader);
13 eva 93 reader.ReadEndElement();
94  
95 reader.ReadStartElement("Value");
16 eva 96 var value = (TValue) valueSerializer.Deserialize(reader);
13 eva 97 reader.ReadEndElement();
98  
99 Add(key, value);
100  
101 reader.ReadEndElement();
102 reader.MoveToContent();
103 }
104 reader.ReadEndElement();
105 }
106  
107 public void WriteXml(XmlWriter writer)
108 {
16 eva 109 var keySerializer = new XmlSerializer(typeof (TKey));
110 var valueSerializer = new XmlSerializer(typeof (TValue));
13 eva 111  
16 eva 112 foreach (var key in Keys)
13 eva 113 {
114 writer.WriteStartElement("Item");
115  
116 writer.WriteStartElement("Key");
117 keySerializer.Serialize(writer, key);
118 writer.WriteEndElement();
119  
120 writer.WriteStartElement("Value");
16 eva 121 var value = this[key];
13 eva 122 valueSerializer.Serialize(writer, value);
123 writer.WriteEndElement();
124  
125 writer.WriteEndElement();
126 }
127 }
128  
129 #endregion
130 }
131 }
132 }