title: C# How to call a method with unknown number of parameters
body: <p>I've reached my skills limit here. I don't even know if this is possible - but I hope it is.</p>
  
  <p>I am making a command handler (text). For each <code>Add()</code> you specify the number of required parameters and their types. Eg:</p>
  
  <pre><code>void Add(string commandName, int requiredParameters, params Type[] paramTypes) { }
  Add("test", 2, typeof(string), typeof(int));
  </code></pre>
  
  <p>So an example command would be: <code>/test hello 7</code>. The command handler checks to make sure the types are right, eg it will fail if the second parameter is not convertible to an int.</p>
  
  <p>Now the problem I'm having is I want to pass a method along in the <code>Add()</code>. (The command handler will call this method if all the checks pass, and calls it with required parameters). So the method in question could have any number of parameters based on what was passed in <code>Add()</code>.</p>
  
  <p>How do I achieve this? A delegate doesn't work at it complain about parameters not matching. I've tried doing something like:</p>
  
  <pre><code>void Add&lt;T1, T2&gt;(..., Action&lt;T1, T2&gt; method) { }
  Add(..., new Action&lt;string, int&gt;(cmd_MyMethod));
  </code></pre>
  
  <p>But I would have to make an Add() method for a lot of types. Eg <code>Add&lt;T1, T2, T3, T4, etc&gt;</code>, and it also makes it a pain to type the calls to <code>Add()</code>.</p>
  
  <p>I <strong>do not</strong> want to pass the method to call as a string, then use <code>this.GetType().GetMethod()</code> to get a handle to it. Although this way would be ideal, it messes up when I do obfuscation. </p>
  
  <p>Does anyone know of any way to do this? 
  Thanks in advance.</p>