I’m trying to do something like this:
public interface IRepository<T>
{
T Get<T>(int id);
}
public interface IFooBarRepository : IRepository<Foo>, IRepository<Bar>
{
}
IFooBarRepository repo = SomeMethodThatGetsTheActualClass();
Foo foo = repo.Get<Foo>(1);
I’m getting a warning:
Type parameter ‘T’ has the same name as the type parameter from outer type ‘IRepository’
And an error:
The call is ambiguous between the following methods or properties: ‘IRepository.Get(int)’ and ‘IRepository.Get(int)’
Any thoughts on how I can make this pattern work?
To call the appropriate one, you’ll need to make the compiler think of the expression in the appropriate way:
Note that you could just cast it in one statement:
… but that looks pretty ugly to me.
That deals with calling the method. Implementing both interfaces in one class is the next hurdle… because they’ll have the same signature in terms of parameters. You’ll have to implement at least one of them explicitly – and it might cause less confusion if you did both:
EDIT: You’ll also need to make
Geta non-generic method: currently you’re trying to redeclare the type parameterTinIRepository<T>.Get<T>; you just want to use the existing type parameter ofIRepository<T>.