title: Using lambda expression to get property OR type name
body: <p>How can I adapt the method below to work when the lambda expression refers to the actual instance itself?</p>
  
  <p>e.g. instead of</p>
  
  <pre><code>x =&gt; x.Name
  </code></pre>
  
  <p>the expression is</p>
  
  <pre><code>x =&gt; x
  </code></pre>
  
  <p>so if I had some class "Car" I could return the string "Car" instead of only being able to operate on its properties (e.g. Car.Colour)</p>
  
  <p>The method:</p>
  
  <pre><code>public static string GetMemberName(Expression expression)
      {
          if (expression is LambdaExpression)
              expression = ((LambdaExpression)expression).Body;
  
          if (expression is MemberExpression)
          {
              var memberExpression = (MemberExpression)expression;
              if (memberExpression.Expression.NodeType ==
                  ExpressionType.MemberAccess)
              {
                  return GetMemberName(memberExpression.Expression)
                         + "."
                         + memberExpression.Member.Name;
              }
              return memberExpression.Member.Name;
          }
  
  
          if (expression is UnaryExpression)
          {
              var unaryExpression = (UnaryExpression)expression;
  
              if (unaryExpression.NodeType != ExpressionType.Convert)
                  throw new Exception(string.Format(
                      "Cannot interpret member from {0}",
                      expression));
              return GetMemberName(unaryExpression.Operand);
          }
          throw new Exception(string.Format(
              "Could not determine member from {0}",
              expression));
      }
  </code></pre>
  
  <p>i.e. I want something like:</p>
  
  <pre><code>if (expression is SomeExpressionThatReturnsAnInstance)
  {
      return (name of type of instance);
  }
  </code></pre>