title: Is there a better option than using dynamic for WPF path indexing?
body: <p>The Microsoft documentation for using indexers in XAML PropertyPaths says to use:</p>
  <pre class="lang-xaml prettyprint-override"><code>&lt;Binding Path=&quot;[key]&quot; ... /&gt;
  </code></pre>
  <p>However <code>key</code> has to be hard coded to use this type of indexing.</p>
  <p>I know the solution for 'flexible' indexing requires <code>MultiBinding</code> of the form:</p>
  <pre class="lang-xaml prettyprint-override"><code>&lt;MultiBinding Converter=&quot;{StaticResource BasicIndexerConverter}&quot;&gt;
      &lt;Binding Path=&quot;Indexable&quot;/&gt;
      &lt;Binding Path=&quot;Key&quot;/&gt;
  &lt;/MultiBinding&gt;
  </code></pre>
  <p>with</p>
  <pre class="lang-cs prettyprint-override"><code>public class BasicIndexerConverter : IMultiValueConverter
  {
      public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
      {
          string[] indexable = (string[])values[0]; //Assuming indexable is type string[]
          int key = (int)values[1];
          return indexable[key];
      }
      public object[] ConvertBack(object values, Type[] targetType, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  <p>If you notice my comment, this will only work for binding on <code>string[]</code> objects. However custom index operators can be created for classes and I would like to generalize this. I can't hardcode every indexer operator key type and return type right? At least not without <code>dynamic</code>.</p>
  <pre class="lang-cs prettyprint-override"><code>public class DynamicIndexerConverter : IMultiValueConverter
  {
      public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
      {
          dynamic indexable = (dynamic)values[0];
          dynamic index = (dynamic)values[1];
          return indexable[index];
      }
  
      public object[] ConvertBack(object values, Type[] targetType, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  <p>This is very simple and I imagine not a terrible use for <code>dynamic</code> considering bindings are resolved at runtime anyway. But I would like to know if there is a static implementation that isn't too complex or would provide the same functionality with better performance. Is this one of those rare use-cases for dynamic?</p>