[英]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.