Object instance by name of class

  • Thread starter Thread starter Ondrej Sevecek
  • Start date Start date
O

Ondrej Sevecek

Hello,
how to create an instance of some object class by using the class's name?
For example, I want to fulfill this pseudocode:

class MyNewClass
{
....
}

Object NewInstanceOfClass = new CreateTheObjectByClassName("MyNewClass");


Many thanks in advance
Ondra.
 
Hello,
how to create an instance of some object class by using the class's
name? For example, I want to fulfill this pseudocode:

class MyNewClass
{
...
}

Object NewInstanceOfClass = new
CreateTheObjectByClassName("MyNewClass");


Many thanks in advance
Ondra.

using System.Reflection;

//if the containing assembly is already loaded
Activator.CreateInstance("MyNamespace.MyClass");


//if the assembly should be loaded at runtime
Assembly a = Assembly.Load(............)
object obj = a.CreateInstance("MyNamespace.MyClass");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top