Take the 2-minute tour ×

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>)

share|improve this question
No. But if it was possible, what methods are you going to call, and how? –  Henk Holterman Oct 24 '12 at 13:28
That doesn't actually make sense. Generics create compile-time types; here, the type would not be known until runtime. You would not be able to do anything with the generic part of the type. –  SLaks Oct 24 '12 at 13:28
I think it might help if you explained more, like why you might want to. –  Justin Harvey Oct 24 '12 at 13:30

6 Answers 6

up vote 9 down vote accepted

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(); :



You could assign the CreateInstance result to a dynamic, and not have to invoke methods through reflection.

dynamic instance = Activator.CreateInstance(genericType);
share|improve this answer
Just about to submit the same answer. Beat me to it. +1 –  Steve Czetty Oct 24 '12 at 13:36
@vzwick Just because it's possible doesn't mean it's a good idea! –  asawyer Oct 24 '12 at 13:47

You can use reflection to create a generic object, for example, you can use a string to find a type (using System.Type.GetType). Once you have a type object, you can use it to find a constructor from the reflection API - see


share|improve this answer

You can use Type.GetType(string) to get a type definition of "Customer". It generally requires at least the full namespace when calling it. Then you can use the type in a call to Type.MakeGenericType(params Type[]) to create a type definition of the generic type. Finally a call to Invoke(object[]) on your newly created type should create an instance of this.

using System;

class Foo<T> where T : class { }

class Bar { }

class Program {
    static void Main(string[] args) {
        var barType = Type.GetType("ConsoleApplication29.Bar");
        var fooType = typeof(Foo<>).MakeGenericType(barType);
        var fooCtor = fooType.GetConstructor(Type.EmptyTypes);
        var instance = fooCtor.Invoke(new object[] { });
share|improve this answer

You can't do it as directly as your example would like it to be, but you can implement a Factory pattern to help you out here. Just pass the string into your factory to return the repository.

EDIT: The factory would have a dictionary of all of the classes that are in the repository (i.e. Customer and Employee) that would get loaded at instantiation. I recommend using attributes and reflection so that you can populate the dictionary dynamically. You then have a method that takes in the string that returns something of type GenericRepository where the string corresponds to one of the types in the dictionary. You then create an instance of that GenericRepository and return it to your caller.

share|improve this answer
Please elaborate! –  vzwick Oct 24 '12 at 13:33

Yes, it is possible. Sample code below, however I would recommend a factory pattern instead.

    internal class Program {

        private static void Main() {

            string customerString = @"Customer";
            string employeeString = @"Employee";

            object customerRepository = GetGenericRepository(customerString);
            object employeeRepository = GetGenericRepository(employeeString);


        public static object GetGenericRepository(string typeName) {

            // get the type from the string
            System.Type type = GetTypeFromName(typeName);

            System.Type repositoryOpenType = typeof(GenericRepository<>);
            System.Type repositoryClosedType = repositoryOpenType.MakeGenericType(type);

            return System.Activator.CreateInstance(repositoryClosedType);

        // there are better methods for getting the type by name
        private static System.Type GetTypeFromName(string typeName) {

            System.Type type = System.Type.GetType(typeName, false);

            if (type == null) {

                var types = from assemblyType in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                            where assemblyType.Name == typeName
                            select assemblyType;

                type = types.FirstOrDefault();

            return type;

    public class Customer {

    public class Employee {

    public class GenericRepository<T> where T : class {
share|improve this answer

Sure you can. You'll probably want an interface to your Repository as well. This serves two purposes. Firstly, you can call the methods in a type-safe language bound manner rather than having to dynamically invoke, and second it'll allow you to mock it easily for testing.

using System;
using System.Collections.Generic;

namespace QuickTest
    public interface IRepository
        void test();
    public class GenericRepositoryFactory
        static public IRepository CreateInstance(params Type[] p)
            Type genericType = typeof(GenericRepository<>).MakeGenericType(p);
            return Activator.CreateInstance(genericType) as IRepository;

    public class GenericRepository<t> : IRepository
        public void test() { Console.WriteLine(this.GetType().ToString()); }

    class Program
        public static void Main(string[] argv)
            var repo = GenericRepositoryFactory.CreateInstance(new[] { typeof(string) });
share|improve this answer

Your Answer


