123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml.Serialization;
- using System.Runtime.Serialization;
- using System.Xml;
- using System.Xml.Schema;
- using System.Reflection;
- using Aitex.Core.RT.Log;
- namespace Aitex.Core.Util
- {
- /// <summary>
- /// Serializable dictionary
- /// </summary>
- /// <typeparam name="KT"></typeparam>
- /// <typeparam name="VT"></typeparam>
- [Serializable]
- public class SerializableDictionary<TKey, TValue>
- : Dictionary<TKey, TValue>, IXmlSerializable
- {
- public SerializableDictionary()
- : base()
- {
- }
- public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
- : base(dictionary)
- {
- }
- public SerializableDictionary(IEqualityComparer<TKey> comparer)
- : base(comparer)
- {
- }
- public SerializableDictionary(int capacity)
- : base(capacity)
- {
- }
- public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
- : base(capacity, comparer)
- {
- }
- protected SerializableDictionary(SerializationInfo info, StreamingContext context)
- : base(info, context)
- {
- }
- /// <summary>
- /// return null
- /// </summary>
- /// <returns></returns>
- public XmlSchema GetSchema()
- {
- return null;
- }
- /// <summary>
- /// Deserialize
- /// </summary>
- /// <param name="reader"></param>
- public void ReadXml(XmlReader reader)
- {
- XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
- XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
- if (reader.IsEmptyElement || !reader.Read())
- {
- return;
- }
- while (reader.NodeType != XmlNodeType.EndElement)
- {
- reader.ReadStartElement("item");
- reader.ReadStartElement("key");
- TKey key = (TKey)keySerializer.Deserialize(reader);
- reader.ReadEndElement();
- reader.ReadStartElement("value");
- TValue value = (TValue)valueSerializer.Deserialize(reader);
- reader.ReadEndElement();
- reader.ReadEndElement();
- reader.MoveToContent();
- this.Add(key, value);
- }
- reader.ReadEndElement();
- }
- /// <summary>
- /// Serialize
- /// </summary>
- /// <param name="writer"></param>
- public void WriteXml(XmlWriter writer)
- {
- try
- {
- XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
- XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
- foreach (TKey key in this.Keys)
- {
- writer.WriteStartElement("item");
- writer.WriteStartElement("key");
- keySerializer.Serialize(writer, key);
- writer.WriteEndElement();
- writer.WriteStartElement("value");
- valueSerializer.Serialize(writer, this[key]);
- writer.WriteEndElement();
- writer.WriteEndElement();
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }
- }
- }
|