编译器 `#error` 以未定义的变量为条件内联,在编译时抛出错误。我怎样才能解决这个问题?

Compiler `#error` inline conditional on undefined variables throwing error on compile. How can I fix this?

我不太确定如何表达这个问题,所以希望以下详细信息能帮助您理解这里到底发生了什么。这是我以前从未遇到过的,代码风格的特殊性对我来说很奇怪。

我下载了 SSH.NET 的源代码,并试图将组件的源代码合并为一个整体项目的一部分,但是有很多块,例如以下块:

        public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeSpan timeout)
        {
#if FEATURE_SOCKET_SYNC
            var totalBytesRead = 0;
            var totalBytesToRead = size;

            socket.ReceiveTimeout = (int) timeout.TotalMilliseconds;

            do
            {
                try
                {
                    var bytesRead = socket.Receive(buffer, offset + totalBytesRead, totalBytesToRead - totalBytesRead, SocketFlags.None);
                    if (bytesRead == 0)
                        return 0;

                    totalBytesRead += bytesRead;
                }
                catch (SocketException ex)
                {
                    if (IsErrorResumable(ex.SocketErrorCode))
                    {
                        ThreadAbstraction.Sleep(30);
                        continue;
                    }

                    if (ex.SocketErrorCode == SocketError.TimedOut)
                        throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture,
                            "Socket read operation has timed out after {0:F0} milliseconds.", timeout.TotalMilliseconds));

                    throw;
                }
            }
            while (totalBytesRead < totalBytesToRead);

            return totalBytesRead;
#elif FEATURE_SOCKET_EAP
            var receiveCompleted = new ManualResetEvent(false);
            var sendReceiveToken = new BlockingSendReceiveToken(socket, buffer, offset, size, receiveCompleted);

            var args = new SocketAsyncEventArgs
            {
                UserToken = sendReceiveToken,
                RemoteEndPoint = socket.RemoteEndPoint
            };
            args.Completed += ReceiveCompleted;
            args.SetBuffer(buffer, offset, size);

            try
            {
                if (socket.ReceiveAsync(args))
                {
                    if (!receiveCompleted.WaitOne(timeout))
                        throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture,
                            "Socket read operation has timed out after {0:F0} milliseconds.", timeout.TotalMilliseconds));
                }

                if (args.SocketError != SocketError.Success)
                        throw new SocketException((int) args.SocketError);

                return sendReceiveToken.TotalBytesTransferred;
            }
            finally
            {
                // initialize token to avoid the waithandle getting used after it's disposed
                args.UserToken = null;
                args.Dispose();
                receiveCompleted.Dispose();
            }
#else
#error Receiving data from a Socket is not implemented.
#endif
        }

在尝试构建时,编译器卡在 #error Receiving data from a Socket is not implemented. 行并抛出异常 CS1029 我可以忽略,但我的担忧更符合为什么所有这些条件未能满足(例如:#elif FEATURE_SOCKET_EAP),我该怎么办?

此外,由于故障转移到 #else 段,我得到 Error CS0161 'SocketAbstraction.Read(Socket, byte[], int, int, TimeSpan)': not all code paths return a value,这迫使编译器抛出带有内联消息的异常(例如:#else <foo> throws以 <foo> 作为描述的 CS1029 异常,并且还在方法中阻止了 return 值)

我能做些什么来纠正这些类型的问题,为什么会有人以这种方式使用内联编译器失败问题?

您必须为指定的条件编译符号之一配置 IDE。

如果您正在使用 Visual Studio:

  • 打开项目属性
  • 转到选项卡 "Build"
  • 在字段 "Conditional compilation symbols" 中输入值 "FEATURE_SOCKET_EAP" 或 "FEATURE_SOCKET_SYNC" 之一。

另请参阅:https://csharp.2000things.com/tag/conditional-compilation/