title: Apply MemberExpression to an object to retrieve Property value
body: <p>I am trying to write a generic function that takes a MemberExpression and an object and returns the value of the Property defined in the member expression.</p>
  
  <p>Here's an example of the code to get the Property name.</p>
  
  <pre><code>public static TProperty GetPropertyName&lt;TModel, TProperty&gt;(Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression, TModel model)
  {
      if (expression.Body is MemberExpression)
      {
          return ((MemberExpression)expression.Body).Member.Name;
      }
      else
      {
          var op = ((UnaryExpression)expression.Body).Operand;
          return ((MemberExpression)op).Member.Name;
      }
  }
  </code></pre>
  
  <p>But I want to retrieve the value of the property from the model:</p>
  
  <pre><code>public static string GetPropertyValue&lt;TModel, TProperty&gt;(Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression, TModel model)
  {
      if (expression.Body is MemberExpression)
      {
          // how do I apply the expression.Body to get the value of the property from model??
  
      }
      else
      {
          var op = ((UnaryExpression)expression.Body).Operand;
          return ((MemberExpression)op).Member.Name;
      }
  }
  </code></pre>
  
  <p>The way I call this function is:</p>
  
  <pre><code>GetPropertyValue&lt;ObjectModel,bool&gt;(m =&gt; m.somebool, m);
  </code></pre>