简体   繁体   English

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

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

I have following methods for serial communication: 我有以下串行通信方法:

private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
  int rxLength = this._serialPort.BytesToRead;
  byte[] rxBuf = new byte[rxLength];
  try
  {
    rxLength = this._serialPort.Read(rxBuf, 0, rxLength);
    this.BeginInvoke((Delegate) (() =>
    {
      this._dataRx.AddBytes(rxBuf, 0, rxLength);
      this.AddDataToAscii(rxBuf, rxLength, true);
    }));
  }
  catch (Exception ex)
  {
  }
}

...and ...和

private void _serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
  this.BeginInvoke((Delegate) (() => { AddDataToAscii("\nSerial Error : " + e.EventType.ToString() + "\n", true); }));
}

Both of this methods returns following error: Severity Code Description Project File Line Suppression State Error CS1660 Cannot convert lambda expression to type 'Delegate' because it is not a delegate type 这两个方法均返回以下错误:严重性代码说明项目文件行抑制状态错误CS1660无法将lambda表达式转换为类型“ Delegate”,因为它不是委托类型

how can I modify the source to define missing delegate type? 如何修改源以定义缺少的委托类型?

Thx. 谢谢。

Action is a Type of Delegate provided by the .NET framework. 操作是.NET框架提供的一种委托类型。 The Action points to a method with no parameters and does not return a value. 该操作指向没有参数的方法,并且不返回任何值。

In your case, to make working your code, replace with Action : 就您而言,要使您的代码起作用,请替换为Action

private void _serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
  this.BeginInvoke((Action) (() => { 
      AddDataToAscii("\nSerial Error : " + e.EventType.ToString() + "\n", true); }));
}

暂无
暂无

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

相关问题 将VB.NET代码转换为C#:无法将Lambda表达式转换为类型“ Delegate”,因为它不是委托类型 - Converting VB.NET code to C#: Cannot convert lambda expression to type 'Delegate' because it is not a delegate type 无法将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表达式转换为委托类型,因为委托不是类型 - Cannot convert lambda expression to type delegate because delegate is not a type “无法将lambda表达式转换为'string'类型,因为它不是委托类型”在C#中查询数据集 - “Cannot convert lambda expression to type 'string' because it is not a delegate type” querying dataset in C# C# 无法将 lambda 表达式转换为“动态”类型,因为它不是委托类型 - C# Cannot convert lambda expression to type 'dynamic' because it is not a delegate type C# Linq 与 MVC,无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型 - C# Linq with MVC, Cannot convert lambda expression to type 'string' because it is not a delegate type C#显示ExpandoObject的数据-无法将lambda表达式转换为类型“…”,因为它不是委托 - C# show data of ExpandoObject - Cannot convert lambda expression to type '…' because it is not a delegate 无法将lambda表达式转换为类型'string',因为它不是具有NotifyOfPropertyChange的委托类型 - Cannot convert lambda expression to type 'string' because it is not a delegate type With NotifyOfPropertyChange
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM