Activator.CreateInstance – How to create instances of classes that have parameterized constructors

There is an overload that accepts arguments as a params object[]:

object obj = Activator.CreateInstance(typeof(StringBuilder), "abc");

Would this do? Alternative, you can use reflection to find the correct constructor:

Type[] argTypes = new Type[] {typeof(string)};
object[] argValues = new object[] {"abc"};
ConstructorInfo ctor = typeof(StringBuilder).GetConstructor(argTypes);
object obj = ctor.Invoke(argValues);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)