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 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
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
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.
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);
}
}
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 ...
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));
}
}
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);
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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). ...
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |