How to pass object [] to Activator.CreateInstance (type, params object [])

I have a class that contains an empty constructor and one that takes an array of objects as a single parameter. Sort of...

public myClass(){ return; }
public myClass(object[] aObj){ return; }

This is the call to the CreateInstance () method that I use

object[] objectArray = new object[5];

// Populate objectArray variable
Activator.CreateInstance(typeof(myClass), objectArray);

he throws System.MissingMethodExceptionwith an added message that reads "Constructor by type" myClass "not found"

A bit of research that I did always showed that the method is called

Activator.CreateInstance(typeof(myClass), arg1, arg2);

Where arg1 and arg2 are types (string, int, bool), and not common objects. How can I call this method only an array of objects as a list of its parameters?

Note. I tried to add another variable to the method signature. Sort of...

public myClass(object[] aObj, bool notUsed){ return; }

. , , , . , ?

+4
2

object:

Activator.CreateInstance(yourType, (object) yourArray);
+12

, :

class YourType {
   public YourType(int[] numbers) {
      ...
   }
}

, , , , params:

int[] yourArray = new int[] { 1, 2, 4 };
Activator.CreateInstance(typeof(YourType ), new object[] { yourArray });
+5

All Articles