'Create empty array using reflection [duplicate]
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?
Solution 1:[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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |