I'm having some troubles to check the proper type on a switch statement. I have these classes that extends from Exception
public abstract class DomainException<T>: Exception
{
public readonly DomainExceptionCodes Code;
public readonly T Metadata;
public DomainException(DomainExceptionCodes code, T metadata): base(message: code.GetEnumDescription())
{
Code = code;
Metadata = metadata;
}
}
public class EmptyFieldsException : DomainException<EmptyFieldsMetadata>
{
public EmptyFieldsException(EmptyFieldsMetadata metadata) : base(code: DomainExceptionCodes.EmptyField, metadata)
{
}
}
Let's asume there is a bunch of derived classes. What I need to do, is to be able to manage all of the derived clases in the same way when handling the exception.
Something like:
try{
// code that raise the exception
}
catch (Exception e) {
// some code here
switch(e) {
case DomainException:
// do something
break;
default:
//other code here
break;
}
}
However, because DomainException
requires at least one argument for the generic type it's raising an error
I tried with object
and dynamic
, but then the type does not match
Any clues?
catch (DomainException e)
will require an argument for the generic type, anddynamic
andobject
won't match. I don't want to have acatch
or acase
for every derived class. I want to use the base class – Rento