title: iterating through an indexed property (Reflection)
body: <p>I want to itterate over an indexed property that I only have access to via reflection, </p>
  
  <p>but ( and I say this in the full knowledge that there's probably an embarrassingly simple answer, MSDN/Google fail =/ ) I cannot find/think of a way besides incrementing a counter over the <code>PropertyInfo.GetValue(prop, counter)</code> until the <code>TargetInvocationException</code> is thrown.</p>
  
  <p>ala:</p>
  
  <pre><code>foreach ( PropertyInfo prop in obj.GetType().GetProperties() )
  {
      if ( prop.GetIndexParameters().Length &gt; 0 )
      {
          // get an integer count value, by incrementing a counter until the exception is thrown
          int count = 0;
          while ( true )
          {
              try
              {
                  prop.GetValue( obj, new object[] { count } );
                  count++;
              }
              catch ( TargetInvocationException ) { break; }
          }
  
          for ( int i = 0; i &lt; count; i++ )
          {
              // process the items value
              process( prop.GetValue( obj, new object[] { i } ) );
          }
      }
  }
  </code></pre>
  
  <p>now, there are some issues with this... very ugly.. solution.. </p>
  
  <p>what if it's multi-dimensional or not indexed by integers for example...</p>
  
  <p>heres the test code I'm using to try and get it working if anyone needs it. If anyone's interested I'm making a custom caching system and .Equals doesn't cut it.</p>
  
  <pre><code>    static void Main()
      {
          object str = new String( ( "Hello, World" ).ToArray() );
  
          process( str );
  
          Console.ReadKey();
      }
  
      static void process( object obj )
      {
          Type type = obj.GetType();
  
          PropertyInfo[] properties = type.GetProperties();
  
          // if this obj has sub properties, apply this process to those rather than this.
          if ( properties.Length &gt; 0 )
          {
              foreach ( PropertyInfo prop in properties )
              {
                  // if it's an indexed type, run for each
                  if ( prop.GetIndexParameters().Length &gt; 0 )
                  {
                      // get an integer count value
                      // issues, what if it's not an integer index (Dictionary?), what if it's multi-dimensional?
                      // just need to be able to iterate through each value in the indexed property
                      int count = 0;
                      while ( true )
                      {
                          try
                          {
                              prop.GetValue( obj, new object[] { count } );
                              count++;
                          }
                          catch ( TargetInvocationException ) { break; }
                      }
  
                      for ( int i = 0; i &lt; count; i++ )
                      {
                          process( prop.GetValue( obj, new object[] { i } ) );
                      }
                  }
                  else
                  {
                      // is normal type so.
                      process( prop.GetValue( obj, null ) );
                  }
              }
          }
          else
          {
              // process to be applied to each property
              Console.WriteLine( "Property Value: {0}", obj.ToString() );
          }
      }
  </code></pre>