Add arguments one by one to a delegate then execute it
<p>Yesterday I wanted to create an <code>ExceptionHandler</code> class to which for example we could pass:</p>

<ul>
<li>the exception</li>
<li>the method where that happened</li>
<li>the parameters given to that method</li>
</ul>

<p>The <code>ExceptionHandler</code> would implement a custom business logic and re-execute automatically (or not) the method with the same parameters.</p>

<p><a href="http://www.codeproject.com/Answers/164917/Pass-a-method-with-N-params-as-parameter-in-C#answer1" rel="nofollow">The closest solution I found</a> was by using a <code>delegate</code> like this</p>

<pre><code>private void button1_Click(object sender, EventArgs e)
{
    InvokeMyMethod(Test, "string", 1);
    InvokeMyMethod(TestN, "other string", 3, Color.Red);
}

public delegate void MyMethodDelegate(params object[] args);

public void InvokeMyMethod(MyMethodDelegate method, params object[] args)
{
    method.DynamicInvoke(args);
}

public void Test(params object[] args)
{
    if (args.Length &lt; 2) return;
    MessageBox.Show(string.Format("{0}{1}", args[0], args[1]));
}

public void TestN(params object[] args)
{
    // or, whatewer
    if (args.Length &lt; 3) return;
    MessageBox.Show(string.Format("{0}{1}{2}", args[0], args[1], args[2]));
}
</code></pre>

<p>However this solution is still not perfect because of the methods' signature. I don't want all my methods receive a unique parameter <code>params object[] args</code>, moreover this is often not possible directly for example with all UI related events (Winform, ASP.NET, WPF or whatever).</p>

<p>So I'd like to know if there is a way to pass one by one arguments to a delegate and then execute the function only when we are ready?</p>

<p>For example here is an imaginary code with non-existing methods <code>AddArgument</code> and <code>Execute</code></p>

<pre><code>public void InvokeMyMethod(MyMethodDelegate method, params object[] args)
{
    foreach(var arg in args)
    {
        method.AddArgument(arg);
    }
    method.Execute();
}
</code></pre>

<p>By this way all our functions could keep their original signature and receive multiple arguments instead of an array.</p>