Fork me on GitHub

XML Serializable Generic Dictionary

    .net 2.0 泛型Dictionary不支持 XML serializable.  下面是一个实现IXmlSerializable 接口实现支持Serialize的泛型集合.Dictionary 。 Dictionary<TKey, TValue>本身实现了ISerializable接口,WebService中无法实现序列化,具体是什么原因它不支持XML序列化。
  

 1    /// <summary>
 2    /// 支持XML序列化的泛型 Dictionary
 3    /// </summary>
 4    /// <typeparam name="TKey"></typeparam>
 5    /// <typeparam name="TValue"></typeparam>

 6    [XmlRoot("dictionary")]
 7    public class SerializableDictionary<TKey, TValue> 
 8        : Dictionary<TKey, TValue>, IXmlSerializable
 9    {
10

 #region 构造函数

        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)
        {

        }

        #endregion
11        IXmlSerializable Members
75
76    }

posted @ 2006-05-24 20:40  张善友  阅读(3902)  评论(5编辑  收藏  举报