博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SerializableDictionary
阅读量:6540 次
发布时间:2019-06-24

本文共 4141 字,大约阅读时间需要 13 分钟。

  1 
using System;
  2 
using System.Runtime.Serialization;
  3 
using System.Xml;
  4 
using System.Xml.Serialization;
  5 
using System.Collections.Generic;
  6 
using System.Text;
  7 
  8 
namespace Sample
  9 {
 10     [Serializable()]
 11     
public 
class SerializableDictionary<TKey, TVal> : Dictionary<TKey, TVal>, IXmlSerializable, ISerializable
 12     {
 13         
#region Constants
 14         
private 
const 
string DictionaryNodeName = 
"
Dictionary
";
 15         
private 
const 
string ItemNodeName = 
"
Item
";
 16         
private 
const 
string KeyNodeName = 
"
Key
";
 17         
private 
const 
string ValueNodeName = 
"
Value
";
 18         
#endregion
 19         
#region Constructors
 20         
public SerializableDictionary()
 21         {
 22         }
 23 
 24         
public SerializableDictionary(IDictionary<TKey, TVal> dictionary)
 25             : 
base(dictionary)
 26         {
 27         }
 28 
 29         
public SerializableDictionary(IEqualityComparer<TKey> comparer)
 30             : 
base(comparer)
 31         {
 32         }
 33 
 34         
public SerializableDictionary(
int capacity)
 35             : 
base(capacity)
 36         {
 37         }
 38 
 39         
public SerializableDictionary(IDictionary<TKey, TVal> dictionary, IEqualityComparer<TKey> comparer)
 40             : 
base(dictionary, comparer)
 41         {
 42         }
 43 
 44         
public SerializableDictionary(
int capacity, IEqualityComparer<TKey> comparer)
 45             : 
base(capacity, comparer)
 46         {
 47         }
 48 
 49         
#endregion
 50         
#region ISerializable Members
 51 
 52         
protected SerializableDictionary(SerializationInfo info, StreamingContext context)
 53         {
 54             
int itemCount = info.GetInt32(
"
ItemCount
");
 55             
for (
int i = 
0; i < itemCount; i++)
 56             {
 57                 KeyValuePair<TKey, TVal> kvp = (KeyValuePair<TKey, TVal>)info.GetValue(String.Format(
"
Item{0}
", i), 
typeof(KeyValuePair<TKey, TVal>));
 58                 
this.Add(kvp.Key, kvp.Value);
 59             }
 60         }
 61 
 62         
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
 63         {
 64             info.AddValue(
"
ItemCount
"
this.Count);
 65             
int itemIdx = 
0;
 66             
foreach (KeyValuePair<TKey, TVal> kvp 
in 
this)
 67             {
 68                 info.AddValue(String.Format(
"
Item{0}
", itemIdx), kvp, 
typeof(KeyValuePair<TKey, TVal>));
 69                 itemIdx++;
 70             }
 71         }
 72 
 73         
#endregion
 74         
#region IXmlSerializable Members
 75 
 76         
void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer)
 77         {
 78             
//
writer.WriteStartElement(DictionaryNodeName);
 79 
            
foreach (KeyValuePair<TKey, TVal> kvp 
in 
this)
 80             {
 81                 writer.WriteStartElement(ItemNodeName);
 82                 writer.WriteStartElement(KeyNodeName);
 83                 KeySerializer.Serialize(writer, kvp.Key);
 84                 writer.WriteEndElement();
 85                 writer.WriteStartElement(ValueNodeName);
 86                 ValueSerializer.Serialize(writer, kvp.Value);
 87                 writer.WriteEndElement();
 88                 writer.WriteEndElement();
 89             }
 90             
//
writer.WriteEndElement();
 91 
        }
 92 
 93         
void IXmlSerializable.ReadXml(System.Xml.XmlReader reader)
 94         {
 95             
if (reader.IsEmptyElement)
 96             {
 97                 
return;
 98             }
 99 
100             
//
 Move past container
101 
            
if (!reader.Read())
102             {
103                 
throw 
new XmlException(
"
Error in Deserialization of Dictionary
");
104             }
105 
106             
//
reader.ReadStartElement(DictionaryNodeName);
107 
            
while (reader.NodeType != XmlNodeType.EndElement)
108             {
109                 reader.ReadStartElement(ItemNodeName);
110                 reader.ReadStartElement(KeyNodeName);
111                 TKey key = (TKey)KeySerializer.Deserialize(reader);
112                 reader.ReadEndElement();
113                 reader.ReadStartElement(ValueNodeName);
114                 TVal value = (TVal)ValueSerializer.Deserialize(reader);
115                 reader.ReadEndElement();
116                 reader.ReadEndElement();
117                 
this.Add(key, value);
118                 reader.MoveToContent();
119             }
120             
//
reader.ReadEndElement();
121 
122             reader.ReadEndElement(); 
//
 Read End Element to close Read of containing node
123 
        }
124 
125         System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
126         {
127             
return 
null;
128         }
129 
130         
#endregion
131         
#region Private Properties
132         
protected XmlSerializer ValueSerializer
133         {
134             
get
135             {
136                 
if (valueSerializer == 
null)
137                 {
138                     valueSerializer = 
new XmlSerializer(
typeof(TVal));
139                 }
140                 
return valueSerializer;
141             }
142         }
143 
144         
private XmlSerializer KeySerializer
145         {
146             
get
147             {
148                 
if (keySerializer == 
null)
149                 {
150                     keySerializer = 
new XmlSerializer(
typeof(TKey));
151                 }
152                 
return keySerializer;
153             }
154         }
155         
#endregion
156         
#region Private Members
157         
private XmlSerializer keySerializer = 
null;
158         
private XmlSerializer valueSerializer = 
null;
159         
#endregion
160     }
161 }

转载地址:http://kysdo.baihongyu.com/

你可能感兴趣的文章
简述思科、华为交换机型号字母代表的意思
查看>>
女神拒绝他可以不用分号
查看>>
memcache--mysql测试
查看>>
拷贝构造函数、拷贝函数、析构函数
查看>>
实战CGLib系列之proxy篇(一):方法拦截MethodInterceptor
查看>>
php 字符串截取
查看>>
ttcn-3
查看>>
00.java虚拟机的基本结构概念
查看>>
深入浅出 ES6:ES6 与 Babel - Broccoli 的联用
查看>>
ThreadLocal使用出现的问题
查看>>
openwrt 常用命令
查看>>
Node.js + Express 4.x + MongoDB 构建登录注册(二)
查看>>
关于十六进制和八进制负数的问题
查看>>
连接池并发的实现原理
查看>>
创建Pch预编译文件
查看>>
阿里云Centos配置iptables防火墙
查看>>
httpclient获取响应实体和信息的封装方法(解耦更新)
查看>>
UML类图几种关系的总结
查看>>
PHP面试题汇总
查看>>
LeetCode (11): Container With Most Water
查看>>