c# Generics. <T>, programmatically assigning T from reading a string value?
c#, generics, instantiation
Solution
Yes, but it's very awkward.
string name = "MyNamespace.Customer";
Type targetType = Type.GetType(name);
Type genericType = typeof(GenericRepository<>).MakeGenericType( targetType );
object instance = Activator.CreateInstance(genericType);
In linqpad, `instance.Dump();` :
GenericRepository<Customer>
UserQuery+GenericRepository`1[UserQuery+Customer]
Edit
You could assign the `CreateInstance` result to a dynamic, and not have to invoke methods through reflection.
dynamic instance = Activator.CreateInstance(genericType);
instance.SomeInstanceMethod(someParameter);
Problem
I have two Classes (i.e. `Customer`, and `Employee`) and a generic repository `GenericRepository<T> where T : class`. Is is possible to instantiate a new GenericRepository instance while assigning the value of T from a string? Like this: ``` string x = "Customer"; var repository = new GenericRepository<x>(); ``` (thus creating a repository instance of type `GenericRepository<Customer>`)