HtmlElementCollection的First()

时间:2014-11-27 02:05:31

标签: c# browser ref

我写了一个方法扩展来模拟List' First()但是如果我尝试更改属性,返回的值(实际引用,isn' t?)不起作用例如,它;

HtmlElement ele = webBrowser1.Document.All.GetElementsByName("foo").First();
ele.InnerText = "hello!"; // doesn't Works. That value isn't changed. Why?
webBrowser1.Document.All.GetElementsByName("foo")[0].InnerText = "abc"; // but this does Works

此处First()功能:

   public static HtmlElement First(this HtmlElementCollection a)
        {
            if (a == null)
                throw new ArgumentNullException();
            if (a.Count == 0)
                throw new InvalidOperationException();

            return a[0];
        }

为什么使用a.First().value = foo的回复并不起作用,arr[0].value = "hehe";呢?我如何解决它?我是否需要了解如何使用退货ref

1 个答案:

答案 0 :(得分:1)

基于这篇文章:LINQ: Select an object and change some properties without creating a new object

var ele = webBrowser1.Document.All.GetElementsByName("foo")
                              .First().Select(e => { e.InnerText = "hello"; return e; });