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

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

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

Aug 29 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Tom Dacon" <td****@community.nospamschrieb
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
Aug 30 '07 #2

P: n/a
Thanks, Armin.

Tom Dacon
Dacon Software Consulting

"Armin Zingler" <az*******@freenet.dewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Tom Dacon" <td****@community.nospamschrieb
>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

Aug 30 '07 #3

P: n/a

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.
Jun 27 '08 #4

P: n/a
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);
}
}
Jun 27 '08 #5

P: n/a
On May 16, 11:02*am, Marc Gravell <marc.grav...@gmail.comwrote:
* * * * *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 ...
Jun 27 '08 #6

P: n/a
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));
}
}
Jun 27 '08 #7

P: n/a
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);
}
}
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.