相关疑难解决方法(0)

这个词典<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
Run Code Online (Sandbox Code Playgroud)

我得出结论,以下代码块以某种方式从输入Dictionary的Keys集合中检索了一个null.但是,输入字典是一个实例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();
}
Run Code Online (Sandbox Code Playgroud)

.net c# dictionary exception

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

.net ×1

c# ×1

dictionary ×1

exception ×1