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

T

Tom Dacon

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
 
A

Armin Zingler

Tom Dacon said:
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?

pi.GetGetMethod.GetParameters
pi.GetSetMethod.GetParameters


Armin
 
S

Saswata Purkayastha

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.
 
M

Marc Gravell

Basically, we are checking whether, the property is an IList(ArrayList may be..),
and then access the individual items.

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);
}
}
 
S

samueltilden

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.
 
S

samueltilden

         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);


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 ...
 
M

Marc Gravell

I tried the above code snippet.

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));
}
}
 
M

Marc Gravell

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);
}
}
 

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