鑑於以下堆棧跟蹤:這個詞典如何<TKey,TValue>例外成爲可能?
MESSAGE: Value cannot be null.Parameter name: key
SOURCE: mscorlib
TARGETSITE: Void ThrowArgumentNullException(System.ExceptionArgument)
STACKTRACE:
at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Collections.Generic.Dictionary'2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary'2.get_Item(TKey key)
at MyCompany.MAF.Agent.ServiceContracts.ConvertUtils.Convert(Dictionary'2 from) in D:\Development\MAF\Agent\MyCompany.MAF.Agent\ServiceContracts\ConvertUtils.cs:line 11
我的結論是莫名其妙下面的代碼塊已檢索從輸入字典的鍵集合空。但是,輸入字典是Dictionary<string, string>
的一個實例。Dictionary<string, string>
的實施使這種情況變得不可能。用空鍵添加項目時,會引發異常。
internal static KeyValuePair<string, string>[] Convert(IDictionary<string, string> from)
{
List<KeyValuePair<string, string>> ret = new List<KeyValuePair<string, string>>();
foreach (string key in from.Keys)
ret.Add(new KeyValuePair<string, string>(key, from[key]));
return ret.ToArray();
}
因爲它沒有真正回答你的問題(我最好的猜測是它是線程相關的)但是你的循環是多餘的from.ToArray()會產生相同的結果 – 2010-02-17 19:04:33