我不确定如何描述这个问题,所以希望下面的细节能帮助你理解这里到底发生了什么。这是我以前从未遇到过的事情,它的代码风格的特殊性对我来说相当奇怪。
我下载了 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
的段,我得到了结果(例如:引发 CS1029 异常,并在方法中阻止返回值)#else
#else <foo>
<foo>
我能做些什么来纠正这些类型的问题,为什么有人会以这种方式使用内联编译器失败问题?