tags:

views:

232

answers:

3

I have a class with a custom indexer like so

public string this[VehicleProperty property]
    {

How can I identify the custom indexer in the results of typeof(MyClass).GetProperties() ?

Thanks.

-Joe

+2  A: 

Look for the DefaultMemberAttribute defined at type level.

(This used to be IndexerNameAttribute, but they seem to have dropped it)

leppie
+3  A: 

You can also look for index parameters, using the the PropertyInfo.GetIndexParameters method, if it returns you than 0 items, that means it's an indexed property:

foreach (PropertyInfo pi in typeof(MyClass).GetProperties())
{
    if (pi.GetIndexParameters().Length > 0)
    {
       // Indexed property...
    }
}
CMS
+1 for beating me to it (I was still typing)
MaLio
+2  A: 
 static void Main(string[] args) {

  foreach (System.Reflection.PropertyInfo propertyInfo in typeof(System.Collections.ArrayList).GetProperties()) {

   System.Reflection.ParameterInfo[] parameterInfos = propertyInfo.GetIndexParameters();
   // then is indexer property
   if (parameterInfos.Length > 0) {
    System.Console.WriteLine(propertyInfo.Name);
   }
  }


  System.Console.ReadKey();
 }
MaLio