【发布时间】:2014-06-14 08:52:19
【问题描述】:
我有返回 IWebElements 列表及其对应名称的代码?我的理解是一个包含两个项目的元组基本上是同一件事,但字典使用哈希映射来关联这两个值。与字典相比,使用双项元组有什么优势,反之亦然?
public Dictionary<IWebElement, string> SelectAllOptions(IWebDriver driver, ref DataObject masterData)
{
//Get the ID of the dropdown menu
DatabaseRetrieval.GetObjectRepository(ref masterData);
var strDropMenuId = masterData.DictObjectRepository["ID"];
//Find the dropdown menu and pull all options into a list
try
{
var dropMenu = new SelectElement(driver.FindElement(By.Id(strDropMenuId)));
// TODO want to know how we want this list to return.
var options = dropMenu.Options as List<IWebElement>;
if (options != null)
{
var values = options.ToDictionary(option => option, option => option.Text);
return values;
}
}
【问题讨论】:
-
A
Tuple<T1, T2>类似于KeyValuePair<TKey, TValue>,而不是字典。 (字典包含一系列键值对。 -
我很好奇为什么当对象已经引用了它自己的名字(
option.Text)时,你甚至需要一个具有相应名称的对象列表...... -
我看的时候也是这么想的,但是同事出于某种原因需要一个列表或者字符串数组。
标签: c# .net dictionary tuples