字典类Dictionary复制
?? 1、實現(xiàn)拷貝一份Dictionary數(shù)據(jù)的類
??? 遍歷原始Dictionay結(jié)構(gòu)和數(shù)據(jù)進行賦值工作,性能很差,通過序列化和反序列化的方式來完成數(shù)據(jù)對象的深度拷貝工作,這種方式快速高效
????[Serializable]
??? public class DictionaryCloneable<TKey, TValue> : Dictionary<TKey, TValue>,IDictionary<TKey, TValue>, ICloneable
??? {
??????? public object Clone()
??????? {
??????????? System.Runtime.Serialization.Formatters.Binary.BinaryFormatter Formatter =
??????????????? new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(null, new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Clone));
??????????? System.IO.MemoryStream stream = new System.IO.MemoryStream();
??????????? Formatter.Serialize(stream, this);
??????????? stream.Position = 0;
??????????? object clonedObj = Formatter.Deserialize(stream);
??????????? stream.Close();
??????????? return clonedObj;
??????? }
??? }
?
實現(xiàn)深度拷貝工作,只需這個類繼承ICloneable接口,通過BinaryFormatter序列化器,首先先將對象實例序列化寫入內(nèi)存流中,然后反序列化流,返回反序列化對象即可。
?
2、特別注意:必須添加一個用于反序列化的構(gòu)造函數(shù),否則會報“未找到反序列化的類型對象的構(gòu)造函數(shù)。”
對于這個錯誤、我們第一個反應是在反序列化的時候找不到默認(無參)的構(gòu)造函數(shù)。但是再看DictionaryCloneable的定義,我們不曾定義任何構(gòu)造函數(shù),意味著它具有一個默認(無參)構(gòu)造函數(shù)。實際上,這里并不是找不到默認(無參)構(gòu)造函數(shù),而是找不到一個具有特殊參數(shù)列表的構(gòu)造函數(shù)。該構(gòu)造函數(shù)接收兩個參數(shù),類型分別是:SerializationInfo和StreamingContext。所以我們的解決方案很簡單,就是加上這么一個構(gòu)造函數(shù)。為此我們從新定義DictionaryCloneable。
?
?[Serializable]
??? public class DictionaryCloneable<TKey, TValue> : Dictionary<TKey, TValue>,IDictionary<TKey, TValue>, ICloneable
??? {
??????? public DictionaryCloneable()
??????? {
??????? }
??????? //必須加上此構(gòu)造函數(shù),在反序列化時被調(diào)用,否則會報“未找到反序列化的類型對象的構(gòu)造函數(shù)。”
??????? public DictionaryCloneable(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
??????????? : base(info, context)
??????? {
??????? }
??????? public object Clone()
??????? {
??????????? System.Runtime.Serialization.Formatters.Binary.BinaryFormatter Formatter =
??????????????? new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(null, new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Clone));
??????????? System.IO.MemoryStream stream = new System.IO.MemoryStream();
??????????? Formatter.Serialize(stream, this);
??????????? stream.Position = 0;
??????????? object clonedObj = Formatter.Deserialize(stream);
??????????? stream.Close();
??????????? return clonedObj;
??????? }
??? }
如果一個類型實現(xiàn)了ISerializable接口(Dictionary<TKey, TValue>就實現(xiàn)了這個接口),你就應該定義如上一個構(gòu)造函數(shù)。這算是一個約定,但是當你繼承某個類型的時候,你往往會忘記這個約定。
?
3、使用
??public volatile Dictionary<int, CommControls.EnumCameraStyle> DicCameraStyleTemp = new Dictionary<int, CommControls.EnumCameraStyle>();
? objec?t obj = DicCameraStyle.Clone();??
??DicCameraStyleTemp = obj as Dictionary<int, CommControls.EnumCameraStyle>;
??DicCameraStyle.Clear();
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的字典类Dictionary复制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xxl-job的使用实例
- 下一篇: 5G边缘计算赋能安防互联网直播行业,青犀