我从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的屏幕截图,语法高亮显示更清晰 - 忽略错误,我正在重构。
答案 0 :(得分:8)
如果这些类继承NotificationException
,就会发生这种情况,因为那时第一个分支总会命中。