Unity3D error: ArgumentException: Key duplication when adding: XXX

今天使用 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)

如圖:
error

出現這種情況的原因是,由兩個併發請求引起的關鍵字重複添加錯誤。

(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);

這樣就可以解決這個錯誤了。(~ ̄▽ ̄)~

參考資料:
http://mono.1490590.n4.nabble.com/System-ArgumentException-Key-duplication-when-adding-httpModules-td4649636.html

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