title: Retrieving Property name from lambda expression
body: <p>Is there a better way to get the Property name when passed in via a lambda expression?
  Here is what i currently have.</p>
  
  <p>eg. </p>
  
  <pre><code>GetSortingInfo&lt;User&gt;(u =&gt; u.UserId);
  </code></pre>
  
  <p>It worked by casting it as a memberexpression only when the property was  a string. because not all properties are strings i had to use object but then it would return a unaryexpression for those.</p>
  
  <pre><code>public static RouteValueDictionary GetInfo&lt;T&gt;(this HtmlHelper html, 
      Expression&lt;Func&lt;T, object&gt;&gt; action) where T : class
  {
      var expression = GetMemberInfo(action);
      string name = expression.Member.Name;
  
      return GetInfo(html, name);
  }
  
  private static MemberExpression GetMemberInfo(Expression method)
  {
      LambdaExpression lambda = method as LambdaExpression;
      if (lambda == null)
          throw new ArgumentNullException("method");
  
      MemberExpression memberExpr = null;
  
      if (lambda.Body.NodeType == ExpressionType.Convert)
      {
          memberExpr = 
              ((UnaryExpression)lambda.Body).Operand as MemberExpression;
      }
      else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
      {
          memberExpr = lambda.Body as MemberExpression;
      }
  
      if (memberExpr == null)
          throw new ArgumentException("method");
  
      return memberExpr;
  }
  </code></pre>