I'm scratching my head to try and make this work, but I'm not sure how to tell the compiler about the types in a way as to avoid explicitly stating them.
Consider a simple interface like this:
interface ICommand<TResult> { }
And a sample implementation:
class SomeCommand : ICommand<int> { }
Now, we have a generic processor interface:
interface ICommandProcessor
{
TResult Process<TCommand, TResult>(TCommand command)
where TCommand : ICommand<TResult>;
}
When I try to use this interface with a concrete command object, I still get a type inference error from the compiler:
Error CS0411 The type arguments for method 'ICommandProcessor.Process(TCommand)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
class Consumer
{
private readonly ICommandProcessor _commandProcessor;
Consumer(ICommandProcessor commandProcessor)
{
_commandProcessor = commandProcessor;
}
public int SomeMethod()
{
return _commandProcessor.Process(new SomeCommand());
}
}
Why is the compiler unable to infer the types in this case, and how can I change the code to enable inference if at all possible?