简体   繁体   English

无法将lambda表达式转换为委托类型,因为委托不是类型

[英]Cannot convert lambda expression to type delegate because delegate is not a type

I'm having a problem that I can't seem to figure out, although its kind of a standard question here on StackOverflow. 我有一个似乎无法解决的问题,尽管在StackOverflow上这是一个标准问题。

base.Invoke(() => prgWork.Value = 100);

What am I doing wrong? 我究竟做错了什么? When I compile it I get the error message: 编译时收到错误消息:

Cannot convert lambda expression to type delegate because delegate is not a type 无法将lambda表达式转换为委托类型,因为委托不是类型

尝试这个:

base.Invoke((Action)(() => { prgWork.Value = 100; }));

Invoke takes a Delegate , rather than a typed Delegate to make the method as generic as possible. Invoke接受一个Delegate ,而不是一个类型化的Delegate来使该方法尽可能通用。 The problem is that type Delegate is an abstract type, and in order for the compiler to infer the number and Types of arguments or the return type of your lambda, you must provide a "concrete" Delegate type. 问题在于类型Delegate是抽象类型,并且为了使编译器能够推断参数的数量和类型或Lambda的返回类型,您必须提供“具体”的Delegate类型。 A common solution to this is to wrap your lambda in an Action delegate, as Action derives from Delegate . 常见的解决方案是将lambda包装在Action委托中,因为ActionDelegate派生的。 In fact, you can pass any type that inherits from Delegate , directly or indirectly, including custom delegates you define in your project. 实际上,您可以直接或间接传递从Delegate继承的任何类型,包括您在项目中定义的自定义委托。

Try something like this: 尝试这样的事情:

base.Invoke(new Action(() => { prgWork.Value = 100; }));

If you find yourself using Invoke often, I would suggest creating an extension method such as this: 如果您发现自己经常使用Invoke ,建议您创建一个扩展方法,例如:

public static void Invoke(this Control control, Action action)
{
    control.Invoke(action);
}

Or with C#6 syntax 或使用C#6语法

public static void Invoke(this Control control, Action action) => control?.Invoke(action);

And now you will be able to use it like this: 现在,您将可以像这样使用它:

someControl.Invoke(() => { prgWork.Value = 100; });

Here the compiler will create a new Action object for you, cleaning up your code. 在这里,编译器将为您创建一个new Action对象,以清理您的代码。 This also sheds some light on why your attempt did not work. 这也为您尝试失败的原因提供了一些启示。 You were basically asking the compiler to create an instance of the abstract class Delegate . 您基本上是在要求编译器创建抽象类Delegate的实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将Lambda表达式转换为类型,因为它不是委托 - Cannot convert lambda expression to type because it is not a delegate 无法将Lambda表达式转换为类型“ Delegate”,因为它不是委托类型MVC 5 - Cannot convert lambda expression to type 'Delegate' because it is not a delegate type MVC 5 无法将lambda表达式转换为“委托”类型,因为它不是委托类型 - Cannot convert lambda expression to type 'Delegate' because it is not a delegate type 无法将lambda表达式转换为类型&#39;string&#39;,因为它不是具有NotifyOfPropertyChange的委托类型 - Cannot convert lambda expression to type 'string' because it is not a delegate type With NotifyOfPropertyChange 无法将 lambda 表达式转换为类型“...”,因为它不是委托类型 - Cannot convert lambda expression to type “…” because it is not a delegate type 无法将 lambda 表达式转换为 int 类型,因为它不是委托类型 - Cannot convert lambda expression to type int because it is not a delegate type 无法将lambda表达式转换为类型“CookieAuthenticationOptions”,因为它不是委托类型 - Cannot convert lambda expression to type 'CookieAuthenticationOptions' because it is not a delegate type 无法将lambda表达式转换为类型“字符串”,因为它不是委托类型? - Cannot convert lambda expression to type “string” because it is not a delegate type? 无法将Lambda表达式转换为“ DbContextOptions”类型 <DataContext> ”,因为它不是委托类型 - Cannot convert lambda expression to type “DbContextOptions <DataContext>” because it is not a delegate type 无法将lambda表达式转换为类型&#39;bool&#39;,因为它不是委托类型错误 - Cannot convert lambda expression to type 'bool' because it is not a delegate type error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM