SerializableDictionary.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Serialization;
  6. using System.Runtime.Serialization;
  7. using System.Xml;
  8. using System.Xml.Schema;
  9. using System.Reflection;
  10. using Aitex.Core.RT.Log;
  11. namespace Aitex.Core.Util
  12. {
  13. /// <summary>
  14. /// Serializable dictionary
  15. /// </summary>
  16. /// <typeparam name="KT"></typeparam>
  17. /// <typeparam name="VT"></typeparam>
  18. [Serializable]
  19. public class SerializableDictionary<TKey, TValue>
  20. : Dictionary<TKey, TValue>, IXmlSerializable
  21. {
  22. public SerializableDictionary()
  23. : base()
  24. {
  25. }
  26. public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
  27. : base(dictionary)
  28. {
  29. }
  30. public SerializableDictionary(IEqualityComparer<TKey> comparer)
  31. : base(comparer)
  32. {
  33. }
  34. public SerializableDictionary(int capacity)
  35. : base(capacity)
  36. {
  37. }
  38. public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
  39. : base(capacity, comparer)
  40. {
  41. }
  42. protected SerializableDictionary(SerializationInfo info, StreamingContext context)
  43. : base(info, context)
  44. {
  45. }
  46. /// <summary>
  47. /// return null
  48. /// </summary>
  49. /// <returns></returns>
  50. public XmlSchema GetSchema()
  51. {
  52. return null;
  53. }
  54. /// <summary>
  55. /// Deserialize
  56. /// </summary>
  57. /// <param name="reader"></param>
  58. public void ReadXml(XmlReader reader)
  59. {
  60. XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
  61. XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
  62. if (reader.IsEmptyElement || !reader.Read())
  63. {
  64. return;
  65. }
  66. while (reader.NodeType != XmlNodeType.EndElement)
  67. {
  68. reader.ReadStartElement("item");
  69. reader.ReadStartElement("key");
  70. TKey key = (TKey)keySerializer.Deserialize(reader);
  71. reader.ReadEndElement();
  72. reader.ReadStartElement("value");
  73. TValue value = (TValue)valueSerializer.Deserialize(reader);
  74. reader.ReadEndElement();
  75. reader.ReadEndElement();
  76. reader.MoveToContent();
  77. this.Add(key, value);
  78. }
  79. reader.ReadEndElement();
  80. }
  81. /// <summary>
  82. /// Serialize
  83. /// </summary>
  84. /// <param name="writer"></param>
  85. public void WriteXml(XmlWriter writer)
  86. {
  87. try
  88. {
  89. XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
  90. XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
  91. foreach (TKey key in this.Keys)
  92. {
  93. writer.WriteStartElement("item");
  94. writer.WriteStartElement("key");
  95. keySerializer.Serialize(writer, key);
  96. writer.WriteEndElement();
  97. writer.WriteStartElement("value");
  98. valueSerializer.Serialize(writer, this[key]);
  99. writer.WriteEndElement();
  100. writer.WriteEndElement();
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. LOG.Write(ex);
  106. }
  107. }
  108. }
  109. }