title: WPF How should I evaluate a property path?
body: <p>I am writing a custom control, and I have a property path as string (think <code>comboBox.SelectedValuePath</code>).<br>
  What is the best way in code to evaluate this string for a arbitrary object?</p>
  
  <p>I obviously can just parse it myself, but it is a hack, and I want the path to support everything <code>comboBox.SelectedValuePath</code> does (for consistency).</p>
  
  <p><strong>Result (thanks to Aran Mulholland):</strong></p>
  
  <p>Not sure about performance of this, but I do not care much for the performance right now.</p>
  
  <pre><code>public class BindingEvaluator {
      #region Target Class
  
      private class Target : DependencyObject {
          public static readonly DependencyProperty ResultProperty = DependencyProperty.Register(
              "Result", typeof(IEnumerable), typeof(BindingEvaluator)
          );
  
          public object Result {
              get { return this.GetValue(ResultProperty); }
              set { this.SetValue(ResultProperty, value); }
          }
      }
  
      #endregion
  
      public object GetValue(object source, string propertyPath) {
          var target = new Target();
          BindingOperations.SetBinding(target, Target.ResultProperty, new Binding(propertyPath) {
              Source = source,
              Mode = BindingMode.OneTime
          });
          return target.Result;
      }
  }
  </code></pre>