The first parameter of Array.CreateInstance
expects the element type of the array. You pass the entire property type, which is, as you have just found out by checking property.PropertyType.IsArray
, an array type (specifically, Bar[]
- i.e. an array of Bar
elements).
To get the element type of an array type, use its GetElementType
method:
var array = Array.CreateInstance(property.PropertyType.GetElementType(), 0);
I suppose you will replace the zero passed to the second argument with a higher number when required, unless you actually want only empty arrays.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…