【发布时间】:2015-01-27 22:58:54
【问题描述】:
我正在尝试遍历一组项目并将它们的属性添加到字典中,但由于某种原因它没有完成循环。
我的代码:
Dictionary<string, Tuple<string, string>> elementList = new Dictionary<string, Tuple<string, string>>();
public void AddElementList(AutomationElementCollection collection)
{
foreach (AutomationElement ele in collection)
{
elementList.Add(ele.Cached.Name, new Tuple<string, string>(ele.Cached.LocalizedControlType.ToString(), ele.Cached.AutomationId.ToString()));
i++;
}
MessageBox.Show(i.ToString());
}
它根本没有进入 MessageBox.Show。通常有大约 52 个项目,它循环通过 8 个然后退出。
【问题讨论】:
-
在调试器中捕获或中断异常,您可能会看到哪里出错了。可能尝试插入重复的键。
-
我最好的猜测是
ele.Cached.Name重复,抛出异常。 -
是的,完全正确,我不敢相信我没想到!
-
既然你的
Tuple只包含两个值,为什么不使用KeyValuePair? .Net 将比Tuple提供更好的支持。 -
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# loops dictionary foreach ui-automation