我不确定如何说出这个问题,所以希望以下细节可以帮助您了解这里究竟发生了什么。这是我以前从未遇到的事情,它在代码风格上的独特性对我来说是相当奇怪的。
我下载了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
),我能做些什么呢?
此外,由于故障转移到Error CS0161 'SocketAbstraction.Read(Socket, byte[], int, int, TimeSpan)': not all code paths return a value
段导致编译器使用内联消息抛出异常(例如:#else
抛出CS1029),我得到#else <foo>
以<foo>
作为描述的异常,并且还阻止方法中的返回值
我可以做些什么来纠正这些类型的问题,以及为什么有人会以这种方式使用内联编译器失败问题?
答案 0 :(得分:1)
您必须为其中一个指定的条件编译符号配置IDE。
如果您使用的是Visual Studio:
另见:https://csharp.2000things.com/tag/conditional-compilation/