title: How to Dynamically Create Method from String for Lambda Expression
body: <p>My ultimate goal is to create a function that will dynamically pass method names to classes in the Hangfire library.</p>
  
  <p>For example, here is the non-dynamic code which works:</p>
  
  <pre><code>RecurringJob.AddOrUpdate(() =&gt; myFunction(), Cron.Hourly)
  </code></pre>
  
  <p>The type of the first argument for AddOrUpdate is <code>Expression&lt;Action&gt;</code>. My first step was to use reflection to dynamically insert the function name:</p>
  
  <pre><code>Type thisControllerType = this.GetType();
  MethodInfo method = thisControllerType.GetMethod(methodName); //methodName passed as string parameter
  RecurringJob.AddOrUpdate(() =&gt; method.Invoke(this, null), Cron.Hourly);
  </code></pre>
  
  <p>When I check the Hangfire dashboard, it seems that this expression is being evaluated as <code>MethodBase.Invoke</code>. So I need help passing in the method name dynamically.</p>
  
  <p>That may be enough info to answer my question, but another path I have taken is trying to generate the entire expression for the argument.</p>
  
  <pre><code>RecurringJob.AddOrUpdate(CreateCallExpression(method), Cron.Hourly);
  
  public Expression&lt;Action&gt; CreateCallExpression(MethodInfo method)
      {
          //trying to pass in zero argument parameter, not sure if this syntax is correct    
          var parameter = System.Linq.Expressions.Expression.Parameter(typeof (Array));
          return System.Linq.Expressions.Expression.Lambda&lt;Action&gt;(System.Linq.Expressions.Expression.Call(method, parameter));
      }
  </code></pre>
  
  <p>In this case I am getting the  exception {"Static method requires null instance, non-static method requires non-null instance.\r\nParameter name: method"}. I am working on that, but not sure if this is the road I should be going down. I have been working on this all day, so I was hoping someone might be able to help me speed up my learning.</p>