【问题标题】:Invalid expression term '.', method "invoke" [duplicate]无效的表达式术语“。”,方法“调用”[重复]
【发布时间】:2019-04-03 13:42:23
【问题描述】:

我正在使用 Visual Studio 2010 和 .net 框架版本 4,我知道之前有人问过这个问题,解决方案是更改 .net 框架,我安装了 .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 出现错误

无效的表达式术语'.'

只有赋值、调用、递增、递减和新对象表达式可以用作语句

有没有建议把Received?.Invoke换成其他形式?

我试过Received != null ? Received.Invoke: null;,但问题没有解决。

【问题讨论】:

  • 我怀疑您使用的是 6.0 之前的 C# 版本(AFAIK 这就是引入空条件运算符的地方)。是否更简单地使用这些编译?如果这些不可用,我会使用简单的if(Received != null) Received.Invoke(this, buf);
  • 广告ReceivedDisconnected是什么
  • "解决方案是更改 .net 框架,"不,它正在更改 语言-版本,这取决于 VS 的版本。
  • @HimBromBeere 如何更改 *language-version * ?
  • @Selvin 我现在在代码中添加了它们。

标签: c# visual-studio-2010


【解决方案1】:

空条件运算符 (?.) 是在 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);
}

【讨论】:

  • 刚刚在链接的答案中看到了 - 已更新。
  • 谢谢你解决了最后一个错误,但是现在我们有另一个“LanmanWorkstation服务无法启动”,我们正在努力,谢谢大家。
猜你喜欢
  • 1970-01-01
  • 2011-06-22
  • 2020-09-23
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多