无法转换lambda表达式&当前上下文中不存在名称“_”

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

有人可以帮我解决吗?我有2种类型的错误信息

  1. 名称'closure_'在当前上下文中不存在
  2. 无法将lambda表达式转换为类型'System.Delegate',因为它不是委托类型

代码是:

private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{
  HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
  string text = this.textBox5.Text;
  htmlDocument.LoadHtml(text);
  HtmlNodeCollection htmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//img/@alt");
  int num1 = 0;
  int k = 0;
  if (htmlNodeCollection == null || this.backgroundWorker3.CancellationPending)
    return;
  string links = "";
  foreach (HtmlNode htmlNode in (IEnumerable<HtmlNode>) htmlNodeCollection)
  {
    HtmlNode aTag = htmlNode;
    int num2 = int.Parse(this.textBox7.Text);
    this._busy.WaitOne(-1);
    if (!this.backgroundWorker3.CancellationPending)
    {
      ++k;
      this.Invoke((Delegate) (() => this.richTextBox4.AppendText(k.ToString() + "." + Environment.NewLine + aTag.InnerHtml + aTag.Attributes["alt"].Value + Environment.NewLine + Environment.NewLine)));
      ++num1;
    }
    this.Invoke((Delegate) (closure_0 ?? (closure_0 = (Action) (() => links = this.richTextBox4.Text + Environment.NewLine))));
    System.IO.File.WriteAllText(this.textBox2.Text + "/Descriptions.txt", links);
    if (num1 == num2)
    {
      this.backgroundWorker3.CancelAsync();
      if (!this.backgroundWorker3.CancellationPending)
        this._busy.Reset();
    }
  }
}

Here is screenshoot

谢谢

c# visual-studio-2013
1个回答
0
投票

这行是无用的代码:

this.Invoke((Delegate) (closure_0 ?? (closure_0 = (Action) (() => links = this.richTextBox4.Text + Environment.NewLine))));

您可以安全地将其替换为:

this.Invoke(new Action(() => { links = this.richTextBox4.Text + Environment.NewLine; }));

您的第一个问题是在您显示的代码中没有名为closure_0的变量或类字段。

其次,您在UI上创建和调用操作的语法是复杂和错误的,使用上面更简单的方法。

© www.soinside.com 2019 - 2024. All rights reserved.