今天使用 Unity 編譯出現一個錯誤, 最後解決了,做個記錄。
error:
ArgumentException: Key duplication when adding: XXX
System.Collections.Hashtable.PutImpl (System.Object key, System.Object value, Boolean overwrite) (at/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections/Hashtable.cs:831)
System.Collections.Hashtable.Add (System.Object key, System.Object value) (at/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections/Hashtable.cs:474)
如圖:
出現這種情況的原因是,由兩個併發請求引起的關鍵字重複添加錯誤。
(C#腳本)
// 這在定義的時候 直接初始化,就會出現這種情況。
private static Hashtable hash = new Hashtable();
hash.Add("AA",AA);
hash.Add("BB",BB);
hash.Add("CC",CC);
// 應該這樣定義,定義hash 初始化爲 null
private static Hashtable hash = nulll;
// 在調用之前,再初始化:
hash = new Hashtable(); // 每添加一行數據 ,初始化一次
hash.Add("AA",AA);
hash.Add("BB",BB);
hash.Add("CC",CC);
這樣就可以解決這個錯誤了。(~ ̄▽ ̄)~