469,628 Members | 1,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

Using reflection to access array elements

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.

Nov 17 '05 #1
3 3808
<tr************@gmail.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
<trevorelbou...@gmail.com> wrote:
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.

Nov 17 '05 #3
<tr************@gmail.com> wrote:
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.


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>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Noah Spitzer-Williams | last post: by
6 posts views Thread by hoover_richard | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.