wasSharp – Diff between revs 25 and 27

Subversion Repositories:
Rev:
Only display areas with differencesRegard whitespace
Rev 25 Rev 27
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 public class SerializableDictionary<TKey, TValue> 21 public class SerializableDictionary<TKey, TValue>
22 : Dictionary<TKey, TValue>, IXmlSerializable 22 : Dictionary<TKey, TValue>, IXmlSerializable
23 { 23 {
24 public XmlSchema GetSchema() 24 public XmlSchema GetSchema()
25 { 25 {
26 return null; 26 return null;
27 } 27 }
28   28  
29 public void ReadXml(XmlReader reader) 29 public void ReadXml(XmlReader reader)
30 { 30 {
31 if (reader.IsEmptyElement) 31 if (reader.IsEmptyElement)
32 { 32 {
33 return; 33 return;
34 } 34 }
35   35  
36 reader.ReadStartElement(); 36 reader.ReadStartElement();
37 while (!reader.NodeType.Equals(XmlNodeType.EndElement)) 37 while (!reader.NodeType.Equals(XmlNodeType.EndElement))
38 { 38 {
39 reader.ReadStartElement(ItemNodeName); 39 reader.ReadStartElement(ItemNodeName);
40   40  
41 reader.ReadStartElement(KeyNodeName); 41 reader.ReadStartElement(KeyNodeName);
42 var key = (TKey) KeySerializer.Deserialize(reader); 42 var key = (TKey)KeySerializer.Deserialize(reader);
43 reader.ReadEndElement(); 43 reader.ReadEndElement();
44   44  
45 reader.ReadStartElement(ValueNodeName); 45 reader.ReadStartElement(ValueNodeName);
46 var value = (TValue) ValueSerializer.Deserialize(reader); 46 var value = (TValue)ValueSerializer.Deserialize(reader);
47 reader.ReadEndElement(); 47 reader.ReadEndElement();
48   48  
49 Add(key, value); 49 Add(key, value);
50   50  
51 reader.ReadEndElement(); 51 reader.ReadEndElement();
52 reader.MoveToContent(); 52 reader.MoveToContent();
53 } 53 }
54 reader.ReadEndElement(); 54 reader.ReadEndElement();
55 } 55 }
56   56  
57 public void WriteXml(XmlWriter writer) 57 public void WriteXml(XmlWriter writer)
58 { 58 {
59 foreach (var key in Keys) 59 foreach (var key in Keys)
60 { 60 {
61 writer.WriteStartElement(ItemNodeName); 61 writer.WriteStartElement(ItemNodeName);
62   62  
63 writer.WriteStartElement(KeyNodeName); 63 writer.WriteStartElement(KeyNodeName);
64 KeySerializer.Serialize(writer, key); 64 KeySerializer.Serialize(writer, key);
65 writer.WriteEndElement(); 65 writer.WriteEndElement();
66   66  
67 writer.WriteStartElement(ValueNodeName); 67 writer.WriteStartElement(ValueNodeName);
68 ValueSerializer.Serialize(writer, this[key]); 68 ValueSerializer.Serialize(writer, this[key]);
69 writer.WriteEndElement(); 69 writer.WriteEndElement();
70   70  
71 writer.WriteEndElement(); 71 writer.WriteEndElement();
72 } 72 }
73 } 73 }
74   74  
75 /// <summary> 75 /// <summary>
76 /// Deep-clones the serializable dictionary. 76 /// Deep-clones the serializable dictionary.
77 /// </summary> 77 /// </summary>
78 /// <returns>a deep clone of the original dictionary</returns> 78 /// <returns>a deep clone of the original dictionary</returns>
79 public SerializableDictionary<TKey, TValue> Clone() 79 public SerializableDictionary<TKey, TValue> Clone()
80 { 80 {
81 SerializableDictionary<TKey, TValue> clone; 81 SerializableDictionary<TKey, TValue> clone;
82 try 82 try
83 { 83 {
84 using (var memoryStream = new MemoryStream()) 84 using (var memoryStream = new MemoryStream())
85 { 85 {
86 new XmlSerializer( 86 new XmlSerializer(
87 typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName)) 87 typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName))
88 .Serialize(memoryStream, this); 88 .Serialize(memoryStream, this);
89 memoryStream.Position = 0; 89 memoryStream.Position = 0;
90 clone = (SerializableDictionary<TKey, TValue>) 90 clone = (SerializableDictionary<TKey, TValue>)
91 new XmlSerializer( 91 new XmlSerializer(
92 typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName)) 92 typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName))
93 .Deserialize(memoryStream); 93 .Deserialize(memoryStream);
94 } 94 }
95 } 95 }
96 /* cloning failed so return an empty dictionary */ 96 /* cloning failed so return an empty dictionary */
97 catch (Exception) 97 catch (Exception)
98 { 98 {
99 clone = new SerializableDictionary<TKey, TValue>(); 99 clone = new SerializableDictionary<TKey, TValue>();
100 } 100 }
101 return clone; 101 return clone;
102 } 102 }
103   103  
104 #region Constants 104 #region Constants
105   105  
106 public string DictionaryNodeName { get; set; } = "Dictionary"; 106 public string DictionaryNodeName { get; set; } = "Dictionary";
107 public string ItemNodeName { get; set; } = "Item"; 107 public string ItemNodeName { get; set; } = "Item";
108 public string KeyNodeName { get; set; } = "Key"; 108 public string KeyNodeName { get; set; } = "Key";
109 public string ValueNodeName { get; set; } = "Value"; 109 public string ValueNodeName { get; set; } = "Value";
110   110  
111 #endregion 111 #endregion Constants
112   112  
113 #region Constructors 113 #region Constructors
114   114  
115 public SerializableDictionary() 115 public SerializableDictionary()
116 { 116 {
117 } 117 }
118   118  
119 public SerializableDictionary(IDictionary<TKey, TValue> dictionary) 119 public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
120 : base(dictionary) 120 : base(dictionary)
121 { 121 {
122 } 122 }
123   123  
124 public SerializableDictionary(IEqualityComparer<TKey> comparer) 124 public SerializableDictionary(IEqualityComparer<TKey> comparer)
125 : base(comparer) 125 : base(comparer)
126 { 126 {
127 } 127 }
128   128  
129 public SerializableDictionary(int capacity) 129 public SerializableDictionary(int capacity)
130 : base(capacity) 130 : base(capacity)
131 { 131 {
132 } 132 }
133   133  
134 public SerializableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) 134 public SerializableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
135 : base(dictionary, comparer) 135 : base(dictionary, comparer)
136 { 136 {
137 } 137 }
138   138  
139 public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer) 139 public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
140 : base(capacity, comparer) 140 : base(capacity, comparer)
141 { 141 {
142 } 142 }
143   143  
144 #endregion 144 #endregion Constructors
145   145  
146 #region Private Properties 146 #region Private Properties
147   147  
148 protected XmlSerializer ValueSerializer 148 protected XmlSerializer ValueSerializer
149 => valueSerializer ?? (valueSerializer = new XmlSerializer(typeof(TValue))); 149 => valueSerializer ?? (valueSerializer = new XmlSerializer(typeof(TValue)));
150   150  
151 private XmlSerializer KeySerializer => keySerializer ?? (keySerializer = new XmlSerializer(typeof(TKey))); 151 private XmlSerializer KeySerializer => keySerializer ?? (keySerializer = new XmlSerializer(typeof(TKey)));
152   152  
153 #endregion 153 #endregion Private Properties
154   154  
155 #region Private Members 155 #region Private Members
156   156  
157 private XmlSerializer keySerializer; 157 private XmlSerializer keySerializer;
158 private XmlSerializer valueSerializer; 158 private XmlSerializer valueSerializer;
159   159  
160 #endregion 160 #endregion Private Members
161 } 161 }
162 } 162 }
163   163  
164
Generated by GNU Enscript 1.6.5.90.
-  
165   -  
166   -  
167   -