title: How can I get my generic method to work?
body: <p>I am writing a small extension to my WPF viewModel base class that allows me to define dependencies between properties in a cleaner way than the standard method of raising multiple <code>PropertyChanged</code> events in the getter:</p>
  
  <p>So instead of:</p>
  
  <pre><code>public int ThisProperty
  {  
      get
      {
          thisProperty = value;
          RaisePropertyChangedEvent("ThisProperty");
          RaisePropertyChangedEvent("FirstDependentProperty");
          RaisePropertyChangedEvent("SecondDependentProperty");
      }
  }
  </code></pre>
  
  <p>I want to be able to do something like this in my ViewModels constructor:</p>
  
  <pre><code>RegisterDependencies("This Property", 
      "FirstDependentProperty", "SecondDependentProperty");
  </code></pre>
  
  <p>I have defined the following method in my viewModel (error checking removed to reduce the amount of code):</p>
  
  <pre><code>public void RegisterDependencies(string property, params string[] dependencies)
  {
      foreach (string item in dependencies)
      {
          IList&lt;string&gt; deps;
          if (dependenciesList.TryGetValue(item, out deps))
          {
              if (!deps.Contains(property))
              {
                  deps.Add(property);
              }
          }
          else
          {
              deps = new List&lt;string&gt;();
              deps.Add(property);
              dependenciesList[item] = deps;
          }
      }
  }
  </code></pre>
  
  <p>My viewmodel subscribes to the <code>PropertyChanged</code> event with the following method:</p>
  
  <pre><code>void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
      IList&lt;string&gt; dependencies;
      if (dependenciesList.TryGetValue(e.PropertyName, out dependencies))
      {
          foreach (string item in dependencies)
          {
              RaisePropertyChangedEvent(item);
          }
      }
  }
  </code></pre>
  
  <p>This simply checks if the property has any dependencies, and if so, raises the <code>PropertyChanged</code> event for those as well. This all works beautifully, so far so good.</p>
  
  <p>What I really want though is to use lambda expressions rather than strings so that I get auto-complete and better re-factoring support. What I'm after is something like this:</p>
  
  <pre><code>RegisterDependencies&lt;ViewModelType&gt;(p =&gt; p.Property, 
      p =&gt; p.FirstDependency,
      p =&gt; p.SecondDependency); //and so on...
  </code></pre>
  
  <p>I have redefined the method signature to look like this:</p>
  
  <pre><code>public void RegisterDependencies&lt;T&gt;(Expression&lt;Func&lt;T, object&gt;&gt; property, 
      prams Expression&lt;Func&lt;T, object&gt;&gt;[] dependencies)
      {
      }
  </code></pre>
  
  <p>At the moment the method simply attempts to convert the expressions to strings and this is where I'm coming unstuck. I'm not sure how to get the name of the property from the expression and convert it to a string.</p>
  
  <p>Josh Smith's MVVM Foundation contains a class called <code>PropertyChangedObserver</code> which has a piece of code that does this which I have attempted to adapt to suit my example:</p>
  
  <pre><code>private static string GetPropertyName&lt;T&gt;(Expression&lt;Func&lt;T, object&gt;&gt; expression)
  {
      var lambda = expression as LambdaExpression;
  
      MemberExpression memberExpression;
      if (lambda.Body is UnaryExpression)
      {
          var unaryExpression = lambda.Body as UnaryExpression;
          memberExpression = unaryExpression.Operand as MemberExpression;
      }
      else
      {
          memberExpression = lambda.Body as MemberExpression;
      }
  
      return memberExpression != null ? ((PropertyInfo)memberExpression.Member).Name : null;
  }
  </code></pre>
  
  <p>This is being called in my updated <code>RegisterDependencies</code> method (this is literally the entire code for the method at the moment):</p>
  
  <pre><code>string propertyName = GetPropertyName(property);
  
  foreach (Expression&lt;Func&lt;T, object&gt;&gt; expr in dependencies)
  {
      string dependencyName = GetPropertyName(expr);
  }
  </code></pre>
  
  <p>When this is run it results in a <code>XamlParseException</code>. This is difficult to debug as it doesn't throw up the standard exception window. Visual Studio's output window provides a bit more information and it seems the initial exception is a <code>InvalidCastException</code> although I'm not sure why as both expressions are of the same type.</p>
  
  <p>Can anyone shed any light and help me with this?</p>