Create a new class with string name

Hello,
I have an abstract class and its derived classes.

I create new derived class with:
MyAbstractClass class1=new DerivedClass(parameter1, parameter2);

It works well but I have to create them with the string name of the derived class.

Something like this :
MyAbstractClass Class1=new StringName(parameter1, parameter2);

How can I do ?
Thanks !

You can use the System.Activator.CreateInstance method to do this.

It might work better using a type instead of a magic string, mind you.

2 Likes

It works, thanks.
Here is my code :

Type t= Type.GetType(MyString);
object[ ] parameters = new object[ ] { parameter1, parameter2 };
var obj = t.Assembly.CreateInstance(MyString, false, BindingFlags.CreateInstance, null, parameters, null, null);
MyAbstractClass Class1= (MyAbstractClass ) obj;