0

For example, I have a class, that looks like:

public class Repository<T>
{
    public IEnumerable<T> FindAll()
    {
        //
    }
}

What I'd like to be able to do, is create an instance of a Repository via reflection.

For example:

var typeName = "Customer"
var type = Assembly.GetCallingAssembly().GetType(typeName);

//obviously, this isn't valid...
var repository = new Repoistory<type>();

Is something along these lines possible?

Alex
  • 37,502
  • 51
  • 204
  • 332
  • here is the answer http://stackoverflow.com/questions/1151464/how-to-dynamically-create-generic-c-sharp-object-using-reflection – opewix Sep 14 '12 at 11:34
  • `.GetType(string);` takes the full type name, with namespace. http://msdn.microsoft.com/en-us/library/y0cd10tb.aspx – Tim S. Sep 14 '12 at 11:45

1 Answers1

1
var repository  = Activator.CreateInstance(typeof(Repository<>).MakeGenericType(type));
L.B
  • 114,136
  • 19
  • 178
  • 224