Cannot convert lamba expression to type "System.Delegate" because it is not a delegate type

I imported the project into Visual Studio, I want to check the source code and compile it, but I get the error mentioned in about 20 different places in the same CS files.

Here is one sample section of code that throws an error: (Error is all overload passed to this .Invoke).

if (this.InvokeRequired) { this.Invoke((Delegate) (() => this.CheckVersionInfo())); } 
+4
source share
2 answers

You can only convert from lambda expressions to specific types of delegates. In this particular case, it is really easy - you can simply use Action instead (or MethodInvoker or any delegate type without parameters and the return type is void ):

 if (this.InvokeRequired) { this.Invoke((Action) (() => this.CheckVersionInfo())); } 

Or just use method group conversion to simplify:

 if (this.InvokeRequired) { this.Invoke((Action)CheckVersionInfo); } 

Alternatively, add an extension method to Control or ISynchronizeInvoke either add the Invoke(Action) method or the check and invoke method. (Both can be useful in different contexts.)

To be honest, I would be very nervous from the rest of the code you import if it is mostly broken like that. If the code is not even compiled to begin with, how much confidence do you have that it will work after fixing the most obvious errors?

Note that this is not just a question of different versions of C # or something like that - the code you posted would never work in any version of C #.

+4
source

Try the following:

 this.Invoke(new MethodInvoker(() => this.CheckVersionInfo())); 
0
source

Source: https://habr.com/ru/post/1497174/


All Articles