Decompiled DLL - CS1660 Cannot convert to 'Delegate' because type is not of delegate type

1k Views Asked by At

I decompiled a .net 4.6.1 project dll with dotpeek. After decompiling I have the following error:

CS1660 Cannot convert to 'Delegate' because type is not of delegate type

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;
      }));
    }

After change with new Action 'Action 1 does not contain a constructor that takes its arguments' '

3

There are 3 best solutions below

3
Caius Jard On BEST ANSWER

I believe it'll work out if you simply change (delegate) to (Action) instead

Before:

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

After:

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

Here's an example:

enter image description here

Edit

You say you have a class called Action already and it's causing a conflict. You can use the full name:

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

Or you can create an alias by e.g. putting this at the top of your class:

using SystemAction = System.Action;

Then using the alias..

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

Or you can rename your class :)

1
Moha Dehghan On

Replace (delegate) with 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;
    }));

The Invoke method accepts a parameter of type Delegate, which is an abstract class and the base type for all delegates.

Lambda expressions may compile to expression trees (Expression<Func<...>>) or plain delegates (Action or Func). The C# compiler needs to know the exact type of the delegate, so it can generate the code for the lambda expression.

By the way, that's the problem with most C# decompilers. I had the best luck with ILSpy.

4
Arcanox On

The value passed to .Invoke() needs to be an actual instantiated delegate type such as Action because the Windows Forms library was created before lambdas existed in C#. Instead of casting the lambda to a delegate like the decompiler wrote, it should read something like this:

this.lstLogBox.Invoke(new Action(() => { ... }));