Hi, my application has the following interfaces:
public IDataContext {
IRepository<T> Repository<T>() where T : class;
}
public interface IRepository<T> where T : class {
T GetById(int id);
}
I have my own implementation setup and initialized using an ioc container. Usually i'd be able to say
var context = ServiceLocator.Current.GetInstance<IDataContext>();
context.Repository<Role>().GetById(1);
Which works fine but i have a case where i don't know the generic type until runtime. Ideally i'd like to be able to say:
Type myType = typeof(Role); // This is just shown as an example
var context = ServiceLocator.Current.GetInstance<IDataContext>();
context.Repository<myType>().GetById(1);
However this does not work. I'd imagine i'd have to use reflection to solve this unless i'm missing something obvious. I'd really appreciate it if someone could help. Thanks
public IDataContext {
IRepository<T> Repository<T>() where T : class;
}
public interface IRepository<T> where T : class {
T GetById(int id);
}
I have my own implementation setup and initialized using an ioc container. Usually i'd be able to say
var context = ServiceLocator.Current.GetInstance<IDataContext>();
context.Repository<Role>().GetById(1);
Which works fine but i have a case where i don't know the generic type until runtime. Ideally i'd like to be able to say:
Type myType = typeof(Role); // This is just shown as an example
var context = ServiceLocator.Current.GetInstance<IDataContext>();
context.Repository<myType>().GetById(1);
However this does not work. I'd imagine i'd have to use reflection to solve this unless i'm missing something obvious. I'd really appreciate it if someone could help. Thanks