1

我用 dotpeek 反编译了一个 .net 4.6.1 项目 dll。反编译后出现以下错误:

CS1660 无法转换为“委托”,因为类型不是委托类型

private void MainLogAdd(string s, System.Drawing.Color color)
    {
      this.logcol.Add(color);
      this.lstLogBox.Invoke((delegate) (() =>
      {
        this.lstLogBox.Items.Add((object) ("[" + DateTime.Now.TimeOfDay.ToString().Substring(0, 8) + "] " + s));
        this.lstLogBox.TopIndex = this.lstLogBox.Items.Count - 1;
      }));
    }

使用新操作更改后“操作 1 不包含采用其参数的构造函数” '

4

3 回答 3

3

我相信如果你只是(delegate)改为(Action)改为

前:

this.lstLogBox.Invoke((delegate) (() =>

后:

this.lstLogBox.Invoke((Action) (() =>

这是一个例子:

在此处输入图像描述

编辑

您说您已经有一个名为 Action 的类,它正在引起冲突。您可以使用全名:

this.lstLogBox.Invoke((System.Action) (() =>

或者,您可以通过将其放在类的顶部来创建别名:

using SystemAction = System.Action;

然后使用别名..

this.lstLogBox.Invoke((SystemAction) (() =>

或者你可以重命名你的班级:)

于 2020-10-26T16:27:35.427 回答
2

替换(delegate)new System.Action

    this.lstLogBox.Invoke(new System.Action(() =>
    {
        this.lstLogBox.Items.Add((object) ("[" + DateTime.Now.TimeOfDay.ToString().Substring(0, 8) + "] " + s));
        this.lstLogBox.TopIndex = this.lstLogBox.Items.Count - 1;
    }));

Invoke方法接受 type 的参数Delegate,这是一个抽象类和所有委托的基类型。

Lambda 表达式可以编译为表达式树 ( Expression<Func<...>>) 或普通委托 (ActionFunc)。C# 编译器需要知道委托的确切类型,因此它可以为 lambda 表达式生成代码。

顺便说一句,这是大多数 C# 反编译器的问题。我在 ILSpy 上遇到了最好的运气。

于 2020-10-26T16:15:34.463 回答
0

传递给的值.Invoke()必须是实际实例化的委托类型,例如Action因为 Windows 窗体库是在 C# 中存在 lambda 之前创建的。与其将 lambda 转换为delegate反编译器所写的那样,它应该是这样的:

this.lstLogBox.Invoke(new Action(() => { ... }));
于 2020-10-26T16:15:42.170 回答