Type error in command template

I am trying to implement a command template with strongly typed input and output parameters for the command itself.

First of all, I created two interfaces that mark input and output for the command:

interface IRequest { } interface IResponse { } 

Then I created the base classes and interfaces. This is an abstract receiver.

 interface IReceiver<TRequest, TResponse> where TRequest : IRequest where TResponse : IResponse { TResponse Action( TRequest request ); } 

and this is an abstract team

 abstract class AbstractCommand<TRequest, TResponse> where TRequest : IRequest where TResponse : IResponse { protected IReceiver<TRequest, TResponse> _receiver; public AbstractCommand( IReceiver<TRequest, TResponse> receiver ) { _receiver = receiver; } public abstract TResponse Execute( TRequest request ); } 

Now I'm trying to use these objects, and so I created the necessary concrete classes

 class TypeARequest : IRequest { public TypeARequest() { } public int NumericValueA { get; set; } public int NumericValueB { get; set; } } class TypeAResponse : IResponse { public TypeAResponse() { } public int Result { get; set; } } class SumCommand : AbstractCommand<TypeARequest, TypeAResponse> { public SumCommand( IReceiver<TypeARequest, TypeAResponse> receiver ) : base( receiver ) { } public override TypeAResponse Execute( TypeARequest request ) { return _receiver.Action( request ); } } class SumReceiver : IReceiver<TypeARequest, TypeAResponse> { public TypeAResponse Action( TypeARequest request ) { return new TypeAResponse() { Result = request.NumericValueA + request.NumericValueB }; } } 

Finally, I created a CommandProcessor class that should be able to handle several commands in general

 class CommandProcessor { IList<AbstractCommand<IRequest, IResponse>> _supportedCommands = new List<AbstractCommand<IRequest, IResponse>>(); public CommandProcessor() { } void AddSupportedCommand( AbstractCommand<IRequest, IResponse> item ) { _supportedCommands.Add( item ); } void SetupSupportedCommands() { // ERROR HERE AddSupportedCommand( new SumCommand( new SumReceiver() ) ); } } 

However, I get a compilation error:

Argument 1: cannot be converted from 'SumCommand' to 'AbstractCommand'

Any help or suggestion?

+5
source share
1 answer

You should create an interface and mark your general parameter as an example of covariance:

 interface IRequest { } interface IResponse { } interface IReceiver<in TRequest, out TResponse> where TRequest : IRequest where TResponse : IResponse { TResponse Action(TRequest request); } interface ICommand<out TRequest, out TResponse> { } abstract class AbstractCommand<TRequest, TResponse> : ICommand<TRequest, TResponse> where TRequest : IRequest where TResponse : IResponse { protected IReceiver<TRequest, TResponse> _receiver; public AbstractCommand(IReceiver<TRequest, TResponse> receiver) { _receiver = receiver; } public abstract TResponse Execute(TRequest request); } class CommandProcessor { IList<ICommand<IRequest, IResponse>> _supportedCommands = new List<ICommand<IRequest, IResponse>>(); public CommandProcessor() { } void AddSupportedCommand(ICommand<IRequest, IResponse> item) { _supportedCommands.Add(item); } void SetupSupportedCommands() { AddSupportedCommand(new SumCommand(new SumReceiver())); } } 

For more information, here is the msdn modifier

+2
source

Source: https://habr.com/ru/post/1271803/


All Articles