3

I have a few classes: SomeClass1, SomeClass2.

How can I create a new instance of one of these classes by using the class name from a string?

Normally, I would do:

var someClass1 = new SomeClass1();

How can I create this instance from the following:

var className = "SomeClass1";

I am assuming I should use Type.GetType() or something but I can't figure it out.

Thanks.

2
  • Is there a known set of classes you would want to do this with? Or could any .NET class be used in this manner in you code? In other words, if I asked you to list all those classes, could you do it easily? Commented May 6, 2010 at 3:25
  • I have a folder of classes (filename match the class name). I am using a T4 template to loop over the directory, chop off the ".cs" and attempt to create an instance of each class. Commented May 6, 2010 at 3:34

3 Answers 3

4
Assembly assembly = Assembly.LoadFrom("SomeDll.dll");    
var myinstance = assembly.CreateInstance("SomeNamespace.SomeClass1");
Sign up to request clarification or add additional context in comments.

4 Comments

That only works if that class is not in a namespace, otherwise you need the full name of the class for CreateInstance to work.
Good catch, left off the namespace
Loading the assembly in the current AppDomain is a bad thing to do, especially if it's already loaded, in which case you'd get an exception. If you are sure it's the same assembly use Activator.CreateInstance. If not, search for the type in the loaded assemblies.
The assembly is already referenced and the namespace is already imported. Is there a way to do this without having to load the assembly again?
2

First you need to get the type through reflection, and then you can create it with the Activator.

To get the type, first figure out what assembly it lives in. For the current assembly where your code is running, see Assembly.GetExecutingAssembly(). For all assemblies loaded in your current AppDomain, see AppDomain.CurrentDomain.GetAssemblies(). Otherwise, see Assembly.LoadFrom.

Then, if you have a class name but no namespace, you can enumerate the types in your assembly through Assembly.GetTypes().

Finally, create the type with Activator.CreateInstance.

using System;
using System.Linq;
using System.Reflection;

namespace ReflectionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly thisAssembly = Assembly.GetExecutingAssembly();
            Type typeToCreate = thisAssembly.GetTypes().Where(t => t.Name == "Program").First();

            object myProgram = Activator.CreateInstance(typeToCreate);

            Console.WriteLine(myProgram.ToString());
        }
    }
}

Comments

0

Why not Dependency Injection frameworks?. This kinds of stuffs are greatly handled by dependency injection framework like Sprint.Net, Structure Map , NInject.....

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.