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");
 
Back
Top