0

How do I create empty array of a type given by reflection? I have tried using the Activator.CreateInstance(Type) method:

Type arrayType = typeof(string[]);
Activator.CreateInstance(arrayType);

but I get a MissingMethodException exception:

System.MissingMethodException: No parameterless constructor defined for this object.

What am I doing wrong?

Dan Stevens
  • 6,392
  • 10
  • 49
  • 68
  • `Type arrayType = typeof(string[]); Array.CreateInstance(arrayType, 5); or you could write Type arrayType = typeof(System.Array); Array.CreateInstance(arrayType, 5)` – MethodMan Jan 08 '16 at 18:09

1 Answers1

-1

Some types you have to trap and handle special, for your case you'd use:

if ( type.IsArray){
  var o = Array.CreateInstance(type, size);
}
T McKeown
  • 12,971
  • 1
  • 25
  • 32