表达式术语“.”无效,方法 "invoke"
Invalid expression term '.', method "invoke"
我正在使用 visual studio 2010 和 .net framework version 4,我知道之前有人问过这个问题,解决方案是更改 .net framewwork,我安装了 .net4.7 但是 visual studio 只接受 .net4.
public void callback(IAsyncResult ar)
{
try
{
sck.EndReceive(ar);
byte[] buf = new byte[8192];
int rec = sck.Receive(buf, buf.Length, 0);
if (rec < buf.Length)
{
Array.Resize<byte>(ref buf, rec);
}
Received?.Invoke(this, buf);//ERROR HERE
sck.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
close();
Disconnected?.Invoke(this);//ERROR HERE
}
}
// recieved and disconnected
public delegate void ClientReceivedHandler(Client_Connexion sender, byte[] data);
public delegate void ClientDisconnectedHandler(Client_Connexion sender);
public event ClientReceivedHandler Received;
public event ClientDisconnectedHandler Disconnected;
在我的代码中出现 Received?.Invoke
错误
Invalid expression term '.'
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
是否有任何建议将 Received?.Invoke
更改为其他形式?
我尝试了Received != null ? Received.Invoke: null;
,但问题没有解决。
空条件运算符 (?.
) 是在 C# 6 中引入的。Visual Studio 2010 仅支持 C# 4。您需要 Visual Studio 2015 或更高版本:您是受限于您的 Visual Studio 版本,而不是您的 .NET 版本。
您必须使用旧模式来引发事件:
var handler = Received; // The temporary copy is very important to avoid race conditions
if (handler != null)
{
handler(this, buf);
}
我正在使用 visual studio 2010 和 .net framework version 4,我知道之前有人问过这个问题,解决方案是更改 .net framewwork,我安装了 .net4.7 但是 visual studio 只接受 .net4.
public void callback(IAsyncResult ar)
{
try
{
sck.EndReceive(ar);
byte[] buf = new byte[8192];
int rec = sck.Receive(buf, buf.Length, 0);
if (rec < buf.Length)
{
Array.Resize<byte>(ref buf, rec);
}
Received?.Invoke(this, buf);//ERROR HERE
sck.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
close();
Disconnected?.Invoke(this);//ERROR HERE
}
}
// recieved and disconnected
public delegate void ClientReceivedHandler(Client_Connexion sender, byte[] data);
public delegate void ClientDisconnectedHandler(Client_Connexion sender);
public event ClientReceivedHandler Received;
public event ClientDisconnectedHandler Disconnected;
在我的代码中出现 Received?.Invoke
Invalid expression term '.'
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
是否有任何建议将 Received?.Invoke
更改为其他形式?
我尝试了Received != null ? Received.Invoke: null;
,但问题没有解决。
空条件运算符 (?.
) 是在 C# 6 中引入的。Visual Studio 2010 仅支持 C# 4。您需要 Visual Studio 2015 或更高版本:您是受限于您的 Visual Studio 版本,而不是您的 .NET 版本。
您必须使用旧模式来引发事件:
var handler = Received; // The temporary copy is very important to avoid race conditions
if (handler != null)
{
handler(this, buf);
}