I have a generic interface like so:
public interface IHandler { T Process<T>(IProcess process) where T : new(); } At times I would like to Implement interface concretely, for example:
public class BoolHandler : IHandler { public bool Process<bool>(IProcess process) { // Do some work - set to true or false return true; } } Is this possible?
EDIT: currently I could have done this:
// Injecct handler in Main and work with single handler ViewModel model = handler.Process<ViewModel>(process); DifferentModel model = handler.Process<DifferentModel >(process); With the suggestions listed I would have to do this (which I'm trying to avoid-it requires me to create bunch of handlers on the fly):
IHandler<ViewModel> handler = new Handler<ViewModel>(); ViewModel viewModel = handler.Process(process); IHandler<DifferentModel> handler = new Handler<DifferentModel>(); // Create yet another handler - arrr DifferentModel viewModel = handler.Process(process);
Tto theIHandlerinterface and remove it from theProcessmethod. Generic parameters on methods need to work uniformly for all applicable types which your implementation cannot.BoolHandlerand the other handlers be used by the same consumer (that wants to consume them polymorphically)? If not, you can have two interfaces, a non-generic interface with a generic method, and a generic interface with a non-generic method.IHandler? e.g.void ConsumeHandler(IHandler handler, ...)? Would you want to passBoolHandler(that wants to implement the method concretely) and other types that want to implement the generic method, interchangeably? If not, then these should have different interfaces.