.NET Framework - Reflection PropertyInfo.GetValue - how to know when it's an indexed property?

Asked By Tom Dacon on 29-Aug-07 07:42 PM
I'm using Reflection to iterate over the properties and fields of an
arbitrary object. For non-indexed properties it's

pi.GetValue(theObject, Nothing) for VB, or pi.GetValue(theObject, null)
for C#

For indexed properties, instead of Nothing (null) you pass an array of index
values.

OK, no problem so far.

But how do you tell that the property is indexed? I've been all over the
PropertyInfo object in the debugger looking for a clue, all over the MSDN
library, and all over Google groups but I can't seem to figure out how to
tell without letting it pull an exception and handling it in the exception
handler - too ugly and slow to be the only way to do it.

This can't be that hard. Any suggestions?

Thanks,
Tom Dacon
Dacon Software Consulting




Armin Zingler replied on 29-Aug-07 10:34 PM
pi.GetGetMethod.GetParameters
pi.GetSetMethod.GetParameters


Armin
Tom Dacon replied on 30-Aug-07 12:12 PM
Thanks, Armin.

Tom Dacon
Dacon Software Consulting
Saswata Purkayastha replied on 16-May-08 10:50 AM
Hi,



The property type can be checked using,



if (propertyInfo.PropertyType == typeof(IList))

{

IList list = (IList)propertyInfo.GetValue (selectedRoleInfo, null);

if (list.Count > 0)

{

.............

.............

}



}



Basically, we are checking whether, the property is an IList(ArrayList may be..),

and then access the individual items.
Marc Gravell replied on 16-May-08 11:02 AM
First - I don't think that is what the OP meant.
Second - very few properties will actually be IList, so the "==
typeof(IList)" check looks dodgy.

I suspect the answer is to look at GetIndexParameters(), as below.

Marc

using System.Reflection;
class Foo
{
public string this[int i]
{
get { return i.ToString(); }
}
static void Main()
{
Foo foo = new Foo();
PropertyInfo pi = typeof(Foo).GetProperty("Item");
// this just to show...
ParameterInfo[] indexes = pi.GetIndexParameters();
object[] indexValues = {1};
object val = pi.GetValue(foo, indexValues);
}
}
samueltilde replied on 18-May-08 08:03 AM
Coincidentally, I am trying to solve the same problem (See <A
HREF="http://groups.google.com/group/
microsoft.public.dotnet.languages.csharp/browse_thread/thread/
e9c5c4084397ecdc#">PropertyInfo.GetValue(object Obj, object[] index)</
A>

In order to determine if a property is an array:

if (PropertyInfo.GetIndexParameters().Length > 0), then the property
is an array.

Then, how to find the values within the array?  That's what I am also
researching.
samueltilde replied on 18-May-08 08:03 AM
I tried the above code snippet.  I got the "Index was outside the
bounds of the array" exception.

I even guessed at different values for object[] indexValues - {0} or
{1} ... same exception ...
Marc Gravell replied on 18-May-08 08:03 AM
To do what? For an indexed property, it works fine. By your other
post, do you mean you tried it with an array? There is a big
difference between an indexer property, and a regular property that
returns an array (which can be accessed by index).

Here's something re arrays...

Marc

using System.Reflection;
using System;
class Foo
{
public int[] SomeVals {
get { return new int[] { 1, 2, 3 }; }
}
static void Main()
{
Foo foo = new Foo();
PropertyInfo pi = typeof(Foo).GetProperty("SomeVals");

// for indexed properties
ParameterInfo[] indexers = pi.GetIndexParameters();
bool isIndexed = indexers.Length > 0;

// for properties that are arrays
bool isArray = pi.PropertyType.IsArray;

// since it isn't indexed and is an array...
// (although I'd probably assume zero-based arrays
// in most common circumstances)
Array arr = (Array) pi.GetValue(foo, null);
object firstValue = arr.GetValue(arr.GetLowerBound(0));
}
}
Marc Gravell replied on 18-May-08 08:03 AM
Actually - I should clarify; the above applies primarily to arrays
(which is what you asked about) - but actually our anonymous friend
(from earlier today) is half-right too; you can use IList, which is
pretty ubiquitious, and covers arrays, collections, lists, etc...

IList list = pi.GetValue(foo, null) as IList;
if (list != null)
{
foreach (object obj in list)
{
Console.WriteLine(obj);
}
}