I have a CommandHandler in my application that has a generic type argument called TCommand
. This generic has a restriction: TCommand : ICommand
.
I have a test project where I test all my commandhandlers. There is an abstract test class for each command handler with some basic (required) functionality. It has a generic type argument called TCommandHandler
. This generic has a restriction as well: TCommandHandler : ICommandHandler<ICommand>
. Sadly, I can't get this to work because the argument I provide does not meet this restriction.
This is my example:
public interface ICommand
{ }
public class Command : ICommand
{ }
public interface ICommandHandler<in TCommand> where TCommand : ICommand
{
Task HandleAsync(TCommand command);
}
public class ExampleCommand : ICommand
{ }
public class ExampleCommandHandler : ICommandHandler<ExampleCommand>
{
//Implementation..
}
Below is my test code:
// Base test class
public abstract class BaseCommandHandlerTest<TCommandHandler>
where TCommandHandler : ICommandHandler<ICommand>
{
// Stuff here
}
public ExampleCommandHandlerTest : BaseCommandHandlerTest<ExampleCommandHandler>
{
// Tests here
}
ExampleCommandHandlerTest
gives me the following error:
The type 'ExampleCommandHandler' cannot be used as type parameter 'TCommandHandler' in the generic type or method 'BaseCommandHandlerTest<TCommandHandler>'. There is no implicit reference conversion from 'AddDecoupledBijlageCommandHandler' to 'ICommandHandler<ICommand>'.
My argument DOES inherit from ICommandHandler
, and the argument's argument (ExampleCommand
) DOES inherit from ICommand
.
Can someone tell me what I am doing wrong here and how I could fix this?
If I can't get it fixed, I will just use a where TCommandHandler : class
constraint because it is not SUPER important; it is merely test code.