title: How to get the value of a ConstantExpression which uses a local variable?
body: <p>I created an ExpressionVisitor implementation that overrides VisitConstant.  However, when I create an expression that utilizes a local variable I can't seem to get the actual value of the variable.  </p>
  
  <pre><code>public class Person
  {
    public string FirstName { get; set; }
  }
  
  string name = "Michael";
  
  Expression&lt;Func&lt;Person, object&gt;&gt; exp = p =&gt; p.FirstName == name;
  </code></pre>
  
  <p>How in the world do I get the value of the variable "name" from the ConstantExpression?
  The only thing that I can think of is this:</p>
  
  <pre><code>string fieldValue = value.GetType().GetFields().First().GetValue(value).ToString();
  </code></pre>
  
  <p>Obviously this doesn't lend itself to being very flexible though....</p>
  
  <p>A slightly more complicated example would be the following:</p>
  
  <pre><code>Person localPerson = new Person { FirstName = "Michael" };
  Expression&lt;Func&lt;Person, object&gt;&gt; exp = p =&gt; p.FirstName == localPerson.FirstName;
  </code></pre>