title: Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'
body: <p>I created a <a href="https://stackoverflow.com/questions/12348472/extracting-method-name-as-string-from-reflection-or-hardcoded-string">method in C#</a> to get methodname</p>
  
  <pre><code>public string GetCorrectPropertyName&lt;T&gt;(Expression&lt;Func&lt;T, string&gt;&gt; expression)
  {
     return ((MemberExpression)expression.Body).Member.Name; // Failure Point
  }
  </code></pre>
  
  <p>and calling it as</p>
  
  <pre><code>string lcl_name = false;
  public string Name
  {
  get { return lcl_name ; }
  set 
      {
          lcl_name = value;
          OnPropertyChanged(GetCorrectPropertyName&lt;ThisClassName&gt;(x =&gt; x.Name));
  }
  }
  </code></pre>
  
  <p>This works fine if property is string and for all other types gives this exception:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'.</p>
  </blockquote>
  
  <ol>
  <li>I changed string to object in method signature, but then it fails again.</li>
  <li>I changed calling from <code>x =&gt; x.PropertyName</code> to <code>x =&gt; Convert.ToString(x.PropertyName)</code> and it still fails</li>
  </ol>
  
  <p>Where am I wrong?</p>