我有錯誤類:
public class FailureException< TRequest, TResponse> : PartialFailureException<TRequest, TResponse>
{
public FailureException(TRequest request, TResponse response, TRequest requestToRetry)
: base(request, response, requestToRetry) { }
}
然后我有不同的類來檢測錯誤的型別:
public static class ErrorDetector
{
public static bool IsFailure(Exception e) =>
e.GetType() == typeof(FailureException<TRequest, TResponse>) )。
}
是否有可能在不添加FailureException<TRequest, TResponse>的情況下檢查錯誤的型別(只使用FailureException),這樣我就不需要在其他一堆類中添加<TRequest, TResponse>了?
uj5u.com熱心網友回復:
我想你可以像這樣用一個開放的泛型來檢查型別:
e.GetType().IsGenericType && e.GetType().GetGenericTypeDefinition() == typeof(FailureException< ,> )。
這里我們不指定型別引數,而是檢查型別是否是通用型別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/320008.html
標籤:
下一篇:如何處理C 中的值多載例外?
