473,804 Members | 3,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reflection PropertyInfo.Ge tValue - 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(the Object, Nothing) for VB, or pi.GetValue(the Object, 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 16573
"Tom Dacon" <td****@communi ty.nospamschrie b
I'm using Reflection to iterate over the properties and fields of an
arbitrary object. For non-indexed properties it's

pi.GetValue(the Object, Nothing) for VB, or pi.GetValue(the Object,
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*******@free net.dewrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
"Tom Dacon" <td****@communi ty.nospamschrie b
>I'm using Reflection to iterate over the properties and fields of an
arbitrary object. For non-indexed properties it's

pi.GetValue(the Object, Nothing) for VB, or pi.GetValue(the Object,
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.P ropertyType == typeof(IList))
{
IList list = (IList)property Info.GetValue (selectedRoleIn fo, 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 GetIndexParamet ers(), as below.

Marc

using System.Reflecti on;
class Foo
{
public string this[int i]
{
get { return i.ToString(); }
}
static void Main()
{
Foo foo = new Foo();
PropertyInfo pi = typeof(Foo).Get Property("Item" );
// this just to show...
ParameterInfo[] indexes = pi.GetIndexPara meters();
object[] indexValues = {1};
object val = pi.GetValue(foo , indexValues);
}
}
Jun 27 '08 #5
On May 16, 11:02*am, Marc Gravell <marc.grav...@g mail.comwrote:
* * * * *Foo foo = new Foo();
* * * * *PropertyInfo pi = typeof(Foo).Get Property("Item" );
* * * * *// this just to show...
* * * * *ParameterInfo[] indexes = pi.GetIndexPara meters();
* * * * *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.Reflecti on;
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).Get Property("SomeV als");

// for indexed properties
ParameterInfo[] indexers = pi.GetIndexPara meters();
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(ar r.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.WriteLi ne(obj);
}
}
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5944
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 answer has ever occoured. This is what I'm trying to do: I wish to build a function that crawls any COM object (interop). I did shortly examine the serializer until I realized it's used for translation
0
1747
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). Here is what I got so far, but I don't know how to tell whether a property is an object or a value type. As you can tell I am doing the entire thing via recursion. private string GetObjectInfo(System.Object o) { string s;
10
7375
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 Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
7
371
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 user input (file values when file is opened). I was hoping to put the name of the property in the tag of each control and fire an event if the a file is open when the control is created and use the tag to get the correct property from the class. ...
3
4415
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 property. All of the web pages implement an interface with this property. In the user control, I was going with Reflection to learn this value but I need help with the code PropertyInfo pi =
8
16925
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. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
6
1206
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 new routine that shows a messagebox with both objects' property values. below is my code. this is the actual comparison code: Dim Properties() As PropertyInfo = pType.GetProperties(BindingFlags.Public Or BindingFlags.Instance) Dim PropertyItem...
5
2829
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 = objControl.GetType().GetMember("cmbComplex").GetType().GetProperty("SelectedValue"); This always returns a value null, and throws an exception hence.
2
7046
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 C# For indexed properties, instead of Nothing (null) you pass an array of index values. OK, no problem so far.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.