mardi 16 avril 2019

I need to rework a method, specifically remove the generic parameter

I have a method that needs a rework, specifically I need to remove the generic parameter in the signature. The method receives a single parameter, which always implements a specific interface.

This is the method:

public void SendCommand(T command) where T : ICommand {
using (var scope = services.CreateScope()) { var commandType = command.GetType(); var handlerType = typeof(ICommandHandler<>).MakeGenericType(commandType);

            var service = scope.ServiceProvider.GetService(handlerType);
            (service as ICommandHandler<T>).Handle(command);
        }

}

The sticking point is the (service as ICommandHandler).Handle(command), line, which receives a type parameter of an object that implements ICommand. Depending on the parameter actual type, the service retrieved is different.

Is there any way to remove the generic parameter, and use the actual type of the parameter as the generic parameter of the ICommandHandler line?





Aucun commentaire:

Enregistrer un commentaire