『臥槽』意外發現了 Hashtable 的 foreach 用法 BUG

這段時間,公司項目中 遇到一個問題,最後查出: 是 Hashtable 的用法導致的。

 

 1         private static void AutoCleanCache()
 2         {
 3             try
 4             {
 5                 lock (m_HashCache.SyncRoot)
 6                 {
 7                     List<object> listKey = new List<object>();
 8                     List<Tuple<string, DateTime, byte[]>> list = new List<Tuple<string, DateTime, byte[]>>();
 9                     if (m_HashCache.Count >= MAXCACHE)
10                     {
11                         //foreach (KeyValuePair<object, object> pair in m_HashCache)  //不能這樣使用 Hashtable —— 可能會導致: 指定的轉換無效 的異常
12                         foreach (object key in m_HashCache.Keys)
13                         {
14                             object value = m_HashCache[key];
15                             var svg = key as string;
16                             var bytes = value as Tuple<DateTime, byte[]>;
17                             if (string.IsNullOrWhiteSpace(svg) || (bytes == null || bytes.Item2 == null || bytes.Item2.Length <= 0)) listKey.Add(key);
18 
19                             list.Add(new Tuple<string, DateTime, byte[]>(svg, (bytes == null ? DateTime.MinValue : bytes.Item1), (bytes == null ? null : bytes.Item2)));
20                         }
21                     }
22 
23                     List<Tuple<string, DateTime, byte[]>> list2 = Enumerable.ToList(list.OrderBy(x => x.Item2).Take(list.Count / 2));
24                     foreach (var item in list2) listKey.Add(item.Item1);
25                     foreach (var key in listKey) m_HashCache.Remove(key);
26 
27                     listKey.Clear();
28                     list.Clear();
29                     list2.Clear();
30                 }
31             }
32             catch (Exception ex)
33             {
34                 logger.Warn(ex);
35             }
36         }

 

代碼很醜 —— 各位不要介意。

我們只看重點代碼:

1 //foreach (KeyValuePair<object, object> pair in m_HashCache) //不能這樣使用 Hashtable —— 可能會導致: 指定的轉換無效 的異常
2 foreach (object key in m_HashCache.Keys)

 

結論:

Hashtable 不要  foreach  KeyValuePair<object, object>

 

 

這下好了, 我這邊 一堆底層代碼 都遭殃了 ~ 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章