0

I have error class:

 public class FailureException<TRequest, TResponse> : PartialFailureException<TRequest, TResponse>
    {
        public FailureException(TRequest request, TResponse response, TRequest requestToRetry)
            : base(request, response, requestToRetry) { }
    }

And then I have different class to detect errors' types:

 public static class ErrorDetector
    {
        public static bool IsFailure(Exception e) =>
            e.GetType() == typeof(FailureException<TRequest, TResponse>));
    }

Is it possible to somwehow check type of error without adding FailureException<TRequest, TResponse> (just using FailureException) so that I wouldn't need to add <TRequest, TResponse> to bunch of other classes?

user122222
  • 2,179
  • 4
  • 35
  • 78
  • 1
    Does [this](https://stackoverflow.com/questions/794198/how-do-i-check-if-a-given-value-is-a-generic-list) answer your question? Just replace `List<>` with `FailureException<,>`. – Sweeper Sep 10 '21 at 07:25

1 Answers1

3

I think you can check for the type with an open generic like this:

e.GetType().IsGenericType && e.GetType().GetGenericTypeDefinition() == typeof(FailureException<,>)

Here we don't specify the type arguments and instead check if the type is the generic type.

juunas
  • 54,244
  • 13
  • 113
  • 149