求助:C# Dictionary遇到的问题。

Jave.Lin Jave.Lin 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
异常描述:

System.ArgumentNullException: Value cannot be null.
Parameter name: key
at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.ContainsKey(TKey key)


public static void CloneKeyTo(ASObject sourceASO, out ASObject desASO)
{
desASO = new ASObject();

string[] keys;
lock (sourceASO.Keys)
{
keys = sourceASO.Keys.ToArray();
}
foreach (string key in keys)
{
lock (sourceASO)
{
if (sourceASO.ContainsKey(key))//这里出异常,说key==null
{
desASO[key] = sourceASO[key];
}
}
}
}


测试代码:

Dictionary<string,object> demoObj = new Dictionary<string,object>();
demoObj[null]=1;//这是更加不可能的,直接异常。


我想问一下:什么情况下,Dictionary<string,object>中,的Keys的元素,中,会有子元素,有可能为:null的情况?

而这个Keys是只读的属性。
...全文
评论 10
收藏 已收藏
分享
返回顶部
Jave.Lin
C# 111,128 成员 642,536 内容