Reflection: How to get the type of an array?

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
 
M

Morten Wennevik [C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top