Reputation: 47587
This seems like basic problem, but I'm struggling with it (maybe because of tiredness).
E.g. - if i create instance of repository like this =>
var repositoryType = typeof(Repository<>).MakeGenericType(entityType);
// repository type==object :(
var repository = ServiceLocator.Current.GetInstance(repositoryType);
What's the best way to call repository.All()
method? Is reflection the only way?
Upvotes: 2
Views: 563
Reputation: 887453
In .Net 3.5, it is not possible to do this without reflection or worse.
Upvotes: 1
Reputation: 1062780
It depends whether Repository<>
exposes some non-generic interface (like ITable
compared to Table<T>
in LINQ-to-SQL). If not, you have to use reflection. If it does, then cast to the non-generic interface:
IRepository repository = (IRepository)ServiceLocator
.Current.GetInstance(repositoryType);
IList data = repository.All();
In 4.0, you could also consider dynamic
:
dynamic repository = ServiceLocator.Current.GetInstance(repositoryType);
IList data = repository.All();
Upvotes: 3