我得到一個TaskCanceledException
:如何找出Exception的基類是否爲OperationCanceledException?
然後,我通過這個例外,因爲Exception
到另一種方法。如果我檢查類型
if (ex.GetType() == typeof(OperationCanceledException))
// ...
他不會介入此if子句。我如何檢查異常的基本類型是否爲OperationCanceledException
?
GetType()
只適用於TaskCanceledException
。 GetType().BaseType
在這裏不可用,也不是IsSubclassOf()
。我現在不在try-catch
了。
'ex is OperationCanceledException' should work –
您是否試過簡單的'if(ex是OperationCanceldException)'?你甚至可以在一個語句中投射它:'if(ex是OperationCanceldException ocex)' – Fildor
Type.BaseType可能是你需要的。這個屬性返回你的對象所在的類型。 – Kinxil