Your code uses the following Activator.CreateInstance Method overload:
public static Object CreateInstance( Type type, params Object[] args )
Pay attention to the params .
Now take a look at your code:
Activator.CreateInstance(type, null)
This passes the null reference as args . In this case, the method searches for a constructor without parameters.
object arg =
This passes a singleton array containing a null reference as args , because arg declared as an object . In this case, the method is looking for a constructor with one parameter.
To avoid any ambiguity, call the method as follows:
object[] args = null; // 0 parameters // - or - object[] args = new object[] { "Hello World" }; // 1 parameter var result = (MyClass)Activator.CreateInstance(type, args);
source share