wasSharp – Diff between revs 10 and 23

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 10 Rev 23
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
6   6  
7 using System; 7 using System;
8 using System.Collections.Generic; 8 using System.Collections.Generic;
9 using System.IO; 9 using System.IO;
10 using System.Xml; 10 using System.Xml;
11 using System.Xml.Schema; 11 using System.Xml.Schema;
12 using System.Xml.Serialization; 12 using System.Xml.Serialization;
13   13  
14 namespace wasSharp.Collections.Generic 14 namespace wasSharp.Collections.Generic
15 { 15 {
16 /// <summary> 16 /// <summary>
17 /// A serializable dictionary class. 17 /// A serializable dictionary class.
18 /// </summary> 18 /// </summary>
19 /// <typeparam name="TKey">the key</typeparam> 19 /// <typeparam name="TKey">the key</typeparam>
20 /// <typeparam name="TValue">the value</typeparam> 20 /// <typeparam name="TValue">the value</typeparam>
21 [XmlRoot("Dictionary")] -  
22 public class SerializableDictionary<TKey, TValue> 21 public class SerializableDictionary<TKey, TValue>
23 : Dictionary<TKey, TValue>, IXmlSerializable 22 : Dictionary<TKey, TValue>, IXmlSerializable
24 { 23 {
25 #region IXmlSerializable Members 24 public XmlSchema GetSchema()
-   25 {
-   26 return null;
-   27 }
26   28  
27 public SerializableDictionary(IEnumerable<KeyValuePair<TKey, TValue>> kvp) 29 public void ReadXml(XmlReader reader)
28 { 30 {
29 foreach (var i in kvp) 31 if (reader.IsEmptyElement)
30 { 32 {
31 Add(i.Key, i.Value); 33 return;
-   34 }
-   35  
-   36 reader.ReadStartElement();
-   37 while (!reader.NodeType.Equals(XmlNodeType.EndElement))
-   38 {
-   39 reader.ReadStartElement(ItemNodeName);
-   40  
-   41 reader.ReadStartElement(KeyNodeName);
-   42 var key = (TKey) KeySerializer.Deserialize(reader);
-   43 reader.ReadEndElement();
-   44  
-   45 reader.ReadStartElement(ValueNodeName);
-   46 var value = (TValue) ValueSerializer.Deserialize(reader);
-   47 reader.ReadEndElement();
-   48  
-   49 Add(key, value);
-   50  
-   51 reader.ReadEndElement();
-   52 reader.MoveToContent();
-   53 }
32 } 54 reader.ReadEndElement();
33 } 55 }
34   56  
-   57 public void WriteXml(XmlWriter writer)
-   58 {
-   59 foreach (var key in Keys)
-   60 {
-   61 writer.WriteStartElement(ItemNodeName);
-   62  
-   63 writer.WriteStartElement(KeyNodeName);
-   64 KeySerializer.Serialize(writer, key);
-   65 writer.WriteEndElement();
-   66  
-   67 writer.WriteStartElement(ValueNodeName);
-   68 ValueSerializer.Serialize(writer, this[key]);
-   69 writer.WriteEndElement();
-   70  
35 public SerializableDictionary() 71 writer.WriteEndElement();
36 { 72 }
37 } 73 }
38   74  
39 /// <summary> 75 /// <summary>
40 /// Deep-clones the serializable dictionary. 76 /// Deep-clones the serializable dictionary.
41 /// </summary> 77 /// </summary>
42 /// <returns>a deep clone of the original dictionary</returns> 78 /// <returns>a deep clone of the original dictionary</returns>
43 public SerializableDictionary<TKey, TValue> Clone() 79 public SerializableDictionary<TKey, TValue> Clone()
44 { 80 {
45 SerializableDictionary<TKey, TValue> clone; 81 SerializableDictionary<TKey, TValue> clone;
46 try 82 try
47 { 83 {
48 using (var writer = new MemoryStream()) 84 using (var memoryStream = new MemoryStream())
49 { 85 {
50 var serializer = -  
51 new XmlSerializer( 86 new XmlSerializer(
52 typeof(SerializableDictionary<TKey, TValue>)); 87 typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName))
53 serializer.Serialize(writer, this); 88 .Serialize(memoryStream, this);
54 writer.Seek(0, SeekOrigin.Begin); 89 memoryStream.Seek(0, SeekOrigin.Begin);
55 clone = (SerializableDictionary<TKey, TValue>) 90 clone = (SerializableDictionary<TKey, TValue>)
56 new XmlSerializer( 91 new XmlSerializer(
57 typeof(SerializableDictionary<TKey, TValue>)) 92 typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName))
58 .Deserialize(writer); 93 .Deserialize(memoryStream);
59 } 94 }
60 } 95 }
61 /* cloning failed so return an empty dictionary */ 96 /* cloning failed so return an empty dictionary */
62 catch (Exception) 97 catch (Exception)
63 { 98 {
64 clone = new SerializableDictionary<TKey, TValue>(); 99 clone = new SerializableDictionary<TKey, TValue>();
65 } 100 }
66 return clone; 101 return clone;
67 } 102 }
68   -  
69 public XmlSchema GetSchema() -  
70 { 103  
71 return null; -  
72 } 104 #region Constants
73   105  
74 public void ReadXml(XmlReader reader) 106 public string DictionaryNodeName { get; set; } = "Dictionary";
75 { 107 public string ItemNodeName { get; set; } = "Item";
76 var keySerializer = new XmlSerializer(typeof(TKey)); -  
77 var valueSerializer = new XmlSerializer(typeof(TValue)); 108 public string KeyNodeName { get; set; } = "Key";
78   109 public string ValueNodeName { get; set; } = "Value";
79 var wasEmpty = reader.IsEmptyElement; -  
80 reader.Read(); 110  
81   111 #endregion
82 if (wasEmpty) 112  
83 return; 113 #region Constructors
84   114  
-   115 public SerializableDictionary()
85 while (!reader.NodeType.Equals(XmlNodeType.EndElement)) 116 {
86 { -  
87 reader.ReadStartElement("Item"); 117 }
88   118  
-   119 public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
-   120 : base(dictionary)
-   121 {
89 reader.ReadStartElement("Key"); 122 }
-   123  
-   124 public SerializableDictionary(IEqualityComparer<TKey> comparer)
90 var key = (TKey) keySerializer.Deserialize(reader); 125 : base(comparer)
91 reader.ReadEndElement(); 126 {
92   127 }
93 reader.ReadStartElement("Value"); -  
94 var value = (TValue) valueSerializer.Deserialize(reader); 128  
-   129 public SerializableDictionary(int capacity)
95 reader.ReadEndElement(); 130 : base(capacity)
96   131 {
97 Add(key, value); 132 }
98   -  
99 reader.ReadEndElement(); -  
100 reader.MoveToContent(); 133  
101 } -  
102 reader.ReadEndElement(); -  
103 } -  
104   134 public SerializableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
105 public void WriteXml(XmlWriter writer) 135 : base(dictionary, comparer)
106 { -  
107 var keySerializer = new XmlSerializer(typeof(TKey)); 136 {
108 var valueSerializer = new XmlSerializer(typeof(TValue)); -  
109   137 }
-   138  
110 foreach (var key in Keys) 139 public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
-   140 : base(capacity, comparer)
111 { 141 {
-   142 }
-   143  
-   144 #endregion
112 writer.WriteStartElement("Item"); 145  
113   146 #region Private Properties
114 writer.WriteStartElement("Key"); 147  
115 keySerializer.Serialize(writer, key); 148 protected XmlSerializer ValueSerializer
116 writer.WriteEndElement(); 149 => valueSerializer ?? (valueSerializer = new XmlSerializer(typeof(TValue)));
117   150  
118 writer.WriteStartElement("Value"); 151 private XmlSerializer KeySerializer => keySerializer ?? (keySerializer = new XmlSerializer(typeof(TKey)));
119 var value = this[key]; 152  
120 valueSerializer.Serialize(writer, value); 153 #endregion
121 writer.WriteEndElement(); 154  
122   155 #region Private Members
123 writer.WriteEndElement(); 156  
124 } 157 private XmlSerializer keySerializer;
125 } 158 private XmlSerializer valueSerializer;
126   159  
127 #endregion 160 #endregion
128 } 161 }
129 } 162 }
130   163  
131
Generated by GNU Enscript 1.6.5.90.
164
Generated by GNU Enscript 1.6.5.90.
132   165  
133   166  
134   167