[英]How to find out if base class of Exception is OperationCanceledException?
您有各种可能性:
is
操作符:
if (ex is OperationCancelledException)
as
运算符(如果您想进一步使用该异常):
OperationCancelledException opce = ex as OperationCancelledException; if (opce != null) // will be null if it's not an OperationCancelledException
IsAssignableFrom
反射(评论说在Xamarin中不起作用):
if (typeof(OperationCancelledException).IsAssignableFrom(ex.GetType())
在C#7中,您可以进行模式匹配:
if (ex is OperationCancelledException opce)
{
// you can use opce here
}
ex is OperationCanceledException
是最佳选择。
但是,如果您确实需要反射/类型对象,请尝试以下操作:
typeof(OperationCanceledException).IsAssignableFrom(ex.GetType())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.