> 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);
}
}
samueltilden@gmail.com - 16 May 2008 17:43 GMT
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.
samueltilden@gmail.com - 16 May 2008 17:56 GMT
> 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 ...
Marc Gravell - 16 May 2008 22:39 GMT
> 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));
}
}
Marc Gravell - 16 May 2008 22:47 GMT
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);
}
}