By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,016 Members | 1,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,016 IT Pros & Developers. It's quick & easy.

How to use reflection to list array values

P: n/a
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[i].Name,
fis[i].GetValue(a));
}
I get System.String[]

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

if( fis[i].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:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
When calling fis[i].GetValue (a), the object returned is an array. So
you can do the following:

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

Hope this helps,

NuTcAsE

Nov 16 '05 #2

P: n/a
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[i].GetValue(a) !! CAST ;-)

Much thanks!
Manfred
"NuTcAsE" <ra********@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
When calling fis[i].GetValue (a), the object returned is an array. So
you can do the following:

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

Hope this helps,

NuTcAsE

Nov 16 '05 #3

P: n/a
Oh yeah... i forgot the cast when calling .GetValue... my bad ;)

NuTcAsE

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.