title: C# Create lambda over given method that injects first paramater
body: <p>In C# I have following methods defined in given class (non static):</p>
  <pre><code>int MyMethod(ScriptEngine script, int a, int b) {
      return a + b;
  }
  
  void MyMethod2(ScriptEngine script, string c) {
      // do something with c
  }
  </code></pre>
  <p>I want to create wrapping lambda / Action / Delegate / MethodInfo (all are accepted by script engine) that automatically passes <code>ScriptEngine</code> and <code>this</code> from given, predefined variables.</p>
  <p>So far I've experimenting with:</p>
  <pre><code>// With overloads up to 16 template parameters
  Action&lt;T1&gt; Wrap&lt;T1&gt;(Action&lt;ScriptEngine, T1&gt; func, ScriptEngine script) { 
       return (Action&lt;T1&gt;) ((t1) =&gt; func(script, t1));
  }
  </code></pre>
  <p>But when called on MyMethod2 I've got <code>The type arguments for method … cannot be inferred from the usage. Try specifying the type arguments explicitly</code>. If I specify the template arguments explicity, it <em>works</em>, but I want to avoid such specification.</p>
  <p>Is there any other way (not neccesery following my solution) I can create such wrapper automatically (or semi-automatically)?</p>
  <p>It is worth mentioning that there is a dedicated abstract method <code>void RegisterAll(ScriptEngine script)</code> that can register required members of given subclass.</p>
  <p>Here is an example of what I am trying to achieve:</p>
  <pre><code>class ScriptEngine { // Stub to have complete example, actual implementation is defined elsewhere
      void RegisterApi(string name, MethodInfo methodInfo) { }
      void RegisterApi(string name, Delegate delegateSelf) { }
  }
  
  class Api {
      int MyMethod(ScriptEngine script, int a, int b) {
          return a + b;
      }
  
      void MyMethod2(ScriptEngine script, string c) {
          // do something with c
      }
  
      void RegisterAll(ScriptEngine script) {
           // Is there any way to shorten this up (not providing MyMethod twice, not providing template arguments?)
          script.RegisterApi(nameof(MyMethod), (Delegate)Wrap&lt;string&gt;(MyMethod, script));
      }
  
  }
  </code></pre>
  <p>The problem is how to improve this <code>RegisterApi</code> method so it:</p>
  <ul>
  <li>Takes the method only once</li>
  <li>Does not require specifying arguments via template method</li>
  </ul>