Groups | Blog | Home
all groups > c# > february 2005 >

c# : How to use reflection to list array values


NuTcAsE
2/22/2005 2:43:00 PM
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
Manfred Braun
2/22/2005 11:26:16 PM
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:_manfred.braun_@manfbraun.de
(Remove the anti-spam-underscore to mail me!)

Manfred Braun
2/23/2005 12:09:27 AM
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


[quoted text, click to view]

NuTcAsE
2/23/2005 7:02:07 AM
Oh yeah... i forgot the cast when calling .GetValue... my bad ;)

NuTcAsE
AddThis Social Bookmark Button