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

Using reflection to access array elements

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
<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

P: n/a
<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

P: n/a
<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.