title: Get property value from MemberExpression
body: <p>I'm attempting to retrieve the value of a property from an instance of <code>MemberExpression</code>. </p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>protected override void VisitMember(Context context, MemberExpression node)
  {              
      var propertyInfo = node.Member as PropertyInfo;
      if(propertyInfo != null)
      {
         var v = propertyInfo.GetValue(node.Member , null);
         val = Convert.ToString(v);
      }
      context.State.Append(val);
  }
  </code></pre>
  
  <p>Depending on the approach I take there are two problems: I don't know the expected type (string, int, etc...), and/or I have not been able access the instance from the <code>MemberExpression</code>.</p>
  
  <p>I am writing a small lambda expressions to T-SQL converter. For example <code>(u)=&gt; u.FirstName == u.LastName;</code> would convert to <code>FirstName = 'chuck'</code>. I've almost got it working!</p>
  
  <p><strong>update</strong></p>
  
  <p>I tried the following code:</p>
  
  <pre><code>...  
  
  var propertyInfo = node.Member as PropertyInfo;
  
  if(propertyInfo != null)
  {
     var o = propertyInfo.GetValue(node.Expression, null);
  }
  
  ...
  </code></pre>
  
  <p>It did not work. I get the following error:</p>
  
  <pre><code>System.Reflection.TargetException : Object does not match target type.
  </code></pre>
  
  <p><strong>update 2</strong></p>
  
  <p>This is what I am trying to accomplish:</p>
  
  <pre><code>    public static Func&lt;T, object&gt; GetValueGetter&lt;T&gt;(this PropertyInfo propertyInfo)
      {
          if (typeof(T) != propertyInfo.DeclaringType)
          {
              throw new ArgumentException();
          }
  
          var instance = Expression.Parameter(propertyInfo.DeclaringType, "i");
          var property = Expression.Property(instance, propertyInfo);
          var convert = Expression.TypeAs(property, typeof(object));
          return (Func&lt;T, object&gt;)Expression.Lambda(convert, instance).Compile();
      }
  </code></pre>
  
  <p>But I do not know <code>T</code> at compile time.</p>