However, you could use duck-typing to "cast" one instance into another. To do this, you need a class representing your duck:
public class LooksLikeAnIServiceRecherche : IServiceRecherche<IUnite, ICritereRechercheUnite> { private readonly dynamic _duck; public LooksLikeAnIServiceRecherche (dynamic duck) { this._duck = duck; } public IList<IUnite> Rechercher(ICritereRechercheUnite critere) { return this._duck.Rechercher(critere); } }
The call to the Rechercher method is checked at runtime, not at compile time, thereby preventing you from getting a compiler error.
Using this code is very simple:
IServiceRechercheUnite rechercheUnite; var serviceRecherche = new LooksLikeAnIServiceRecherche(rechercheUnite);
For more information on how to use dynamic -keyword, see MSDN: dynamic
source share