//public class ThreadSafeDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICloneable
[Serializable]
public class ThreadSafeDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ICloneable
{
public object Clone()
{
BinaryFormatter Formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
MemoryStream stream = new MemoryStream();
Formatter.Serialize(stream, this);
stream.Position = 0;
object clonedObj = Formatter.Deserialize(stream);
stream.Close();
return clonedObj;
}
public ThreadSafeDictionary() { }
//父类实现了ISerializable接口的,子类也必须有序列化构造函数,否则反序列化时会出错。
protected ThreadSafeDictionary(SerializationInfo info, StreamingContext context)
: base(info, context)
{
//Value = info.GetBoolean("Test_Value");
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
//info.AddValue("Test_Value", Value);
}
//public bool Value { get; set; }
}
这时,才可以序列化和反序列化这个Dictionary.