简体   繁体   English

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

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

I am having trouble with an anonymous delegate lambda in C#. 我在C#中遇到匿名委托lambda时遇到问题。 I just converted the app to C#5 and the delegates went all haywire on me. 我刚刚将应用程序转换为C#5,代表们对我一切都失控了。 Any help would be great. 任何帮助都会很棒。 The specific error is: 具体错误是:

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

public void UpdateUserList()
{
  if (!Monitor.TryEnter((object)this.LvPerson, 150))
      return;

  if (this.InvokeRequired)
  {
      this.Invoke((Delegate) (() => this.UpdateUserList()));              
  }
  else
  { ... }
}

I have also tried 我也试过了

this.Invoke(() => {this.UpdateUserList();});

I'm not sure where the issue is as this was working before I moved the project from Visual Studio 2008 to Visual Studio 2015. 在将项目从Visual Studio 2008移动到Visual Studio 2015之前,我不确定问题出在哪里。

Thanks again for the help! 再次感谢您的帮助!

The Invoke method expects a Delegate type instance, because you use a lambda expression it cannot automatically translate the expression into something like new Delegate() because Delegate has no public constructors. Invoke方法需要一个Delegate类型实例,因为你使用lambda表达式它不能自动将表达式转换为类似新的Delegate(),因为Delegate没有公共构造函数。 Using 运用

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

Should solve the problem as Action is a subclass of Delegate. 应该解决问题,因为Action是Delegate的子类。 To get rid of the redundant new Action(...) when using Invoke you can write a set of extension methods that take as argument an Action, this way the new Action(...) will be handled by the C# compiler so you won't have to write it every time making your code cleaner. 要在使用Invoke时摆脱冗余的新Action(...),你可以编写一组扩展方法,将Action作为参数,这样新的Action(...)将由C#编译器处理,所以你每次使代码更清洁时都不必写它。

In case you are using Invoke for some asynchronous operations that may involve other threads look into Task Parallel Library (TPL) and Task-based Asynchronous Pattern (TAP), the latter has built in support into C# and Visual Basic.NET, using await will no longer require a call to Invoke() and allow you to run some operations on the background freeing your UI. 如果您正在使用Invoke进行某些可能涉及其他线程的异步操作,请查看任务并行库(TPL)和基于任务的异步模式(TAP),后者已经内置了对C#和Visual Basic.NET的支持,使用await将不再需要调用Invoke()并允许您在后台运行某些操作来释放UI。

暂无
暂无

声明:本站的技术帖子网页,遵循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 delegate is not a 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