Reflection: How to get the type of an array?

  • Thread starter Thread starter Achim Domma
  • Start date Start date
A

Achim Domma

Hi,

I'm analyzing the fields of an object like this:

foreach (FieldInfo info in obj.GetType().GetFields()) {
...
}

Some of the fields are arrays, so I check for info.IsArray which works
fine and info.FieldType contains MyType[] for example. But how do I get
the base type of the array, i.e. MyType? Must be simple, but I could not
figure out how to do it!?

regards,
Achim
 
Hi Achim,

You can use Type.GetElementType() to find out what type the array
contains. You might also need to check for Type.HasElementTypes first.


Hi,

I'm analyzing the fields of an object like this:

foreach (FieldInfo info in obj.GetType().GetFields()) {
...
}

Some of the fields are arrays, so I check for info.IsArray which works
fine and info.FieldType contains MyType[] for example. But how do I get
the base type of the array, i.e. MyType? Must be simple, but I could not
figure out how to do it!?

regards,
Achim
 
Back
Top