I want to have an interface specify that any implementations of that interface must use a subtype of a specific interface in their method declaration:
interface IModel {} // The original type
interface IMapper {
void Create(IModel model); // The interface method
}
So now I want my implementation of this interface to expect not IModel itself, but a subtype of IModel:
public class Customer : IModel {} // My subtype
public class CustomerMapper : IMapper {
public void Create(Customer customer) {} // Implementation using the subtype
}
At the moment I’m getting the following error:
‘CustomerMapper’ does not implement interface member ‘IMapper.Create(IModel)’
Is there a way I can achieve this?
You need to make your interface generic in the type of value it should expect:
If you don’t make it generic, anything which only knows about the interface couldn’t know what kind of model would be valid.