How to use reflection to list array values

M

Manfred Braun

Hi All,

I try to list the values from an objects array member:

[ one of a's member is an array]

FieldInfo[] fis = a.GetType().GetFields(BindingFlags.Public |
BindingFlags.Instance);
for(int i = 0; i < fis.Length; i++)
{
Console.WriteLine("Name:{0},Value:{1}", fis.Name,
fis.GetValue(a));
}
I get System.String[]

I know I can figure out, if a field is an array:

if( fis.FieldType.IsArray) ...

But how to get the values ??? I have no problem to obtain simple values!

Any help would be great!

Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
N

NuTcAsE

When calling fis.GetValue (a), the object returned is an array. So
you can do the following:

object[] val = fis.GetValue (a);
foreach (object o in val) {
Console.WriteLine (o.ToString());
}

Hope this helps,

NuTcAsE
 
M

Manfred Braun

Hi NuTcAsE,

and thanks for your help!! I took you as a person, who has the experience to
do this ;-)
But your code felt, like mine before. But that, that I trust you lead me now
to the right direction:

don't forget the cast [also the compiler is very accurate and means this
!!]:

object[] val = (object[]) fis.GetValue(a) !! CAST ;-)

Much thanks!
Manfred
 

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