需要帮助打印字符串列表

问题描述 投票:0回答:2

我正在尝试打印类 xyz 的所有内部文本值,但这就是我打印的所有内容“System.Collections.Generic.List`1[System.String]

    public List<String> getL1Names()
    {

        UITestControl document = browinX.CurrentDocumentWindow;
        HtmlControl control = new HtmlControl(document);
        control.SearchProperties.Add(HtmlControl.PropertyNames.Class, "xyz");
        UITestControlCollection controlcollection = control.FindMatchingControls();
        List<string> names = new List<string>();
        foreach (HtmlControl link in controlcollection)
        {
            if (link is HtmlHyperlink)
            names.Add(control.InnerText);
        }
        return names;
    }

用它来打印

Console.WriteLine(siteHome.getL1Names());
c# visual-studio list return coded-ui-tests
2个回答
1
投票

“System.Collections.Generic.List`1[System.String]

那是因为

System.Collections.Generic.List<T>
不会重载 ToString()。默认实现(继承自 System.Object)会打印对象类型的名称,这就是您所看到的。

您可能想迭代列表中的所有元素,并分别打印每个元素。

你可以改变

Console.WriteLine(siteHome.getL1Names());

类似的事情

foreach (var name in siteHome.getL1Names()) 
{
    Console.WriteLine(name);
}

0
投票

您可以像这样打印字符串列表:

string stringList = string.Join(",", siteHome.getL1Names().Select(x => x));
Console.WriteLine(stringList);
© www.soinside.com 2019 - 2024. All rights reserved.