Using reflection to access array elements

T

trevorelbourne

Hi,

I am having trouble accessing the elements of an array using
reflection. This is the code I am having trouble with:

FieldInfo[] Fields = Obj.GetType().GetFields();
foreach (FieldInfo fi in Fields)
{
Object Temp = fi.GetValue(Obj);
if (Temp.GetType().IsArray)
{
// This is an array type.
MethodInfo GetLength =
Temp.GetType().GetMethod("GetLength");
Params = new Object[1];
int Length = (int)GetLength.Invoke(Temp, Params);
Object[] a = (Object[])Temp;
for (i = 0; i < Length; i++)
{
// Process array elements
}
}
}

I get a runtime error when executing the line:
Object[] a = (Object[])Temp;

The error message is:
InvalidCastException was not handled.
Specified cast is not valid.

Any ideas on what the problem is?

In the above case, it turns out the actual array is of type "Int32[]",
so if I modify the above line to become:
Int32[] a = (Int32[])Temp;

It works fine.

Any help greatfully received. Thanks.

Trevor.
 
J

Jon Skeet [C# MVP]

I am having trouble accessing the elements of an array using
reflection. This is the code I am having trouble with:

FieldInfo[] Fields = Obj.GetType().GetFields();
foreach (FieldInfo fi in Fields)
{
Object Temp = fi.GetValue(Obj);
if (Temp.GetType().IsArray)
{
// This is an array type.
MethodInfo GetLength =
Temp.GetType().GetMethod("GetLength");
Params = new Object[1];
int Length = (int)GetLength.Invoke(Temp, Params);
Object[] a = (Object[])Temp;
for (i = 0; i < Length; i++)
{
// Process array elements
}
}
}

For one thing, you're not putting anything into your parameter array -
shouldn't you put in the value 0?
I get a runtime error when executing the line:
Object[] a = (Object[])Temp;

The error message is:
InvalidCastException was not handled.
Specified cast is not valid.

Any ideas on what the problem is?

In the above case, it turns out the actual array is of type "Int32[]",
so if I modify the above line to become:
Int32[] a = (Int32[])Temp;

It works fine.

Well that's the problem - an array of ints isn't an array of objects.
You can't cast from object[] to int[] regardless of reflection.

I don't see why you're using reflection in the first place though - why
not just cast to Array or IList and access the members that way?
 
T

trevorelbourne

I am having trouble accessing the elements of an array using
reflection. This is the code I am having trouble with:

FieldInfo[] Fields = Obj.GetType().GetFields();
foreach (FieldInfo fi in Fields)
{
Object Temp = fi.GetValue(Obj);
if (Temp.GetType().IsArray)
{
// This is an array type.
MethodInfo GetLength =
Temp.GetType().GetMethod("GetL­ength");
Params = new Object[1];
int Length = (int)GetLength.Invoke(Temp, Params);
Object[] a = (Object[])Temp;
for (i = 0; i < Length; i++)
{
// Process array elements
}
}
}

For one thing, you're not putting anything into your parameter
array - shouldn't you put in the value 0?

No. The call to "Invoke" method in the above code is calling the
"GetLength()" method for an Array type, which itself has no paramaters.
I get a runtime error when executing the line:
Object[] a = (Object[])Temp;

The error message is:
InvalidCastException was not handled.
Specified cast is not valid.


Any ideas on what the problem is?


In the above case, it turns out the actual array is of type "Int32[]",
so if I modify the above line to become:
Int32[] a = (Int32[])Temp;


It works fine.

Well that's the problem - an array of ints isn't an array
of objects. You can't cast from object[] to int[] regardless
of reflection.

I don't see why you're using reflection in the first place
though - why not just cast to Array or IList and access the
members that way?

I am using reflection because the above code is part of a Serialization
class I am writing because the .NET compact framework doesn't provide
serialization.

Yeah, I realised that I should be using Array. Thanks.

Trevor.
 
J

Jon Skeet [C# MVP]

No. The call to "Invoke" method in the above code is calling the
"GetLength()" method for an Array type, which itself has no paramaters.

Yes it does - the dimension to return the length of. Look up
Array.GetLength in MSDN. If it *didn't* take any parameters, you would
have to set params to new object[0] rather than new object[1].

<snip>
 

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