How to use class name as parameter in C#

Object createObjectBy(Type clazz){
   // .. do construction work here
    Object theObject = Activator.CreateInstance(clazz);
    return theObject;
}

Usage:

createObjectBy(typeof(A));

Or you could simply use Activator.CreateInstance directly 🙂

Leave a Comment