You can do that with reflection, for example:
Type repoType = typeof(Repository<>).MakeGenericType(yourType);
but reflection soon begats more reflection - as you'd then need to talk about repoType
via reflection. There are ways of minimising this impact, though:
- non-generic interfaces (the simplest approach)
- calling a generic method once via reflection that does all the additional work via regular static generic code
For the first, I might have an IRepository
interface (non-generic), then it is just:
IRepository repo = (IRepository)Activator.CreateInstance(repoType);
repo.SomeNonGenericMethods(args); // etc