Resharper 声明 if/else 条件永远不会被命中,为什么?
Resharper states if/else condition will never be hit, why?
我从 PushSharp 示例中获得了这段代码,它详细说明了如何处理异常。由于某种原因,Resharper 将所有 else
条件显示为灰色,并指出 The expression is always false
。我不明白这怎么可能。
// ex is an Exception passed in to the method
if (ex is NotificationException)
{
// Deal with the failed notification
var notification = ((NotificationException)ex).Notification;
var logItem = new PushLog($"{typePrefix} Notification failed", $"Notification Failed: {notification}");
_pushLogRepo.Insert(logItem);
}
else if (ex is DeviceSubscriptionExpiredException) // Resharper says this is always false
{
// exception handling code...
}
else if (ex is RetryAfterException) // Resharper says this is always false
{
// exception handling code...
}
else
{
Console.WriteLine("Notification Failed for some (Unknown Reason)");
}
谁能解释一下这是怎么可能的?我不明白怎么可能。这是 VS2015 的屏幕截图,语法突出显示更清晰 - 忽略错误,我正在重构中。
如果那些 类 继承 NotificationException
,就会发生这种情况,从那时起第一个分支总是会命中。
我从 PushSharp 示例中获得了这段代码,它详细说明了如何处理异常。由于某种原因,Resharper 将所有 else
条件显示为灰色,并指出 The expression is always false
。我不明白这怎么可能。
// ex is an Exception passed in to the method
if (ex is NotificationException)
{
// Deal with the failed notification
var notification = ((NotificationException)ex).Notification;
var logItem = new PushLog($"{typePrefix} Notification failed", $"Notification Failed: {notification}");
_pushLogRepo.Insert(logItem);
}
else if (ex is DeviceSubscriptionExpiredException) // Resharper says this is always false
{
// exception handling code...
}
else if (ex is RetryAfterException) // Resharper says this is always false
{
// exception handling code...
}
else
{
Console.WriteLine("Notification Failed for some (Unknown Reason)");
}
谁能解释一下这是怎么可能的?我不明白怎么可能。这是 VS2015 的屏幕截图,语法突出显示更清晰 - 忽略错误,我正在重构中。
如果那些 类 继承 NotificationException
,就会发生这种情况,从那时起第一个分支总是会命中。