c# retrieving type of array element using reflection

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.

Leave a Comment