473,382 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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
7 16504
"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
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Lars Nielsen | last post by:
Hi everybody, I've run into a problem when using the reflection assembly on COM objects and been browsing around the net for some while. Apparently a few has asked the same question though no...
0
by: Frank Rizzo | last post by:
How can I do deep reflection on an object. In other words, if a property of an object is another object, I want to do reflection on that too and so on until I arrive at value types (or string). ...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
3
by: Eric Sabine | last post by:
This is for a web app but I figured it's more of a lanugage Q: I have a user control which is going to be used by multiple web pages. In the web control I need to get the bool value of a public...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
6
by: ECathell | last post by:
I have a routine that compares 2 intances of the same object to see if there are any changes. The problem is that even though there are no changes, it tells me there are. I have even tried running a...
5
by: Skandy | last post by:
Hello All: I'm stuck with this. I'm trying to obtain the SelectedValue property for a combobox control on UserControl using reflection. So I write: PropertyInfo pi =...
2
by: Tom Dacon | last post by:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.