Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Dacon - 30 Aug 2007 00:42 GMT
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
Armin Zingler - 30 Aug 2007 03:34 GMT
> I'm using Reflection to iterate over the properties and fields of an
> arbitrary object. For non-indexed properties it's
[quoted text clipped - 15 lines]
>
> This can't be that hard. Any suggestions?

   pi.GetGetMethod.GetParameters
   pi.GetSetMethod.GetParameters

Armin
Tom Dacon - 30 Aug 2007 17:12 GMT
Thanks, Armin.

Tom Dacon
Dacon Software Consulting

>> I'm using Reflection to iterate over the properties and fields of an
>> arbitrary object. For non-indexed properties it's
[quoted text clipped - 20 lines]
>
> Armin
Saswata Purkayastha - 16 May 2008 15:50 GMT
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.
Marc Gravell - 16 May 2008 16:02 GMT
> 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);
           }
       }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.