title: Get the parameter value from a Linq Expression
body: <p>I have the following class</p>
  
  <pre><code>public class MyClass
  {
      public bool Delete(Product product)
      {
          // some code.
      }
  }
  </code></pre>
  
  <p>Now I have a helper class that looks like this</p>
  
  <pre><code>public class Helper&lt;T, TResult&gt;
  {
  
      public Type Type;
      public string Method;
      public Type[] ArgTypes;
      public object[] ArgValues;
  
      public Helper(Expression&lt;Func&lt;T, TResult&gt;&gt; expression)
      {
          var body = (System.Linq.Expressions.MethodCallExpression)expression.Body;
  
          this.Type = typeof(T);
          this.Method = body.Method.Name;
          this.ArgTypes = body.Arguments.Select(x =&gt; x.Type).ToArray();
          this.ArgValues = ???
      }
  }
  </code></pre>
  
  <p>The idea ist to use this code from somewhere:</p>
  
  <pre><code>// I am returning a helper somewhere
  public Helper&lt;T&gt; GetMethod&lt;T&gt;()
  {
      var product = GetProduct(1);
      return new Helper&lt;MyClass&gt;(x =&gt; x.Delete(product));
  }
  
  // some other class decides, when to execute the helper 
  // Invoker already exists and is responsible for executing the method
  // that is the main reason I don't just comile and execute my Expression
  public bool ExecuteMethod&lt;T&gt;(Helper&lt;T&gt; helper)
  {
      var instance = new MyClass();
      var Invoker = new Invoker(helper.Type, helper.Method, helper.ArgTypes, helper.ArgValues);
      return (bool)Invoker.Invoke(instance);
  }
  </code></pre>
  
  <p>The point where I am stuck is how to extract the arguments from the expression itself.</p>
  
  <p>I found this way</p>
  
  <pre><code>((ConstantExpression)((MemberExpression)body.Arguments[0]).Expression).Value
  </code></pre>
  
  <p>which seems to be an object type with a field "product" but I believe there must be a simpler solution.</p>
  
  <p>Any suggestions.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Just to clarify, I modified my code according to what I want to achive. In my real word application I already have a class that does the same but without an expression tree:</p>
  
  <pre><code>var helper = new Helper(typeof(MyClass), "Delete", 
      new Type[] { typeof(Product) }, new object[] {product}));
  </code></pre>
  
  <p>The main reason for my <code>Helper&lt;T&gt;</code> is to have Compile-Time checking if the method signature is valid.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>This is my current implementation, is there a better way to acces the values, without using reflection?</p>
  
  <pre><code>public Helper(Expression&lt;Func&lt;T, TResult&gt;&gt; expression)
  {
      var body = (System.Linq.Expressions.MethodCallExpression)expression.Body;
  
      this.Type = typeof(T);
      this.Method = body.Method.Name;
      this.ArgTypes = body.Arguments.Select(x =&gt; x.Type).ToArray();
  
      var values = new List&lt;object&gt;();
      foreach(var arg in body.Arguments)
      {
          values.Add(
              (((ConstantExpression)exp.Expression).Value).GetType()
                  .GetField(exp.Member.Name)
                  .GetValue(((ConstantExpression)exp.Expression).Value);
          );
      }
      this.ArgValues = values.ToArray();
  }
  </code></pre>