title: How do you invoke a method with any signature dynamically in C#?
body: <p>I had a class with some common error handling code, and I wanted to pass in the method and arguments to call, but I couldn't quite come up with the syntax.  What I want to do is roughly this:</p>
  
  <pre><code>private void InvokeHelper(Delegate method, params object[] args)
  {
    bool retry = false;
  
    do
    {
      try
      {
        method.DynamicInvoke(args);
        retry = false;
      }
      catch (MyException ex)
      {
        retry = HandleException(ex);
      }
    } while (retry);
  }
  </code></pre>
  
  <p>and then be able to do things like:</p>
  
  <pre><code>InvokeHelper(foo.MethodA, a, b, c);
  InvokeHelper(foo.MethodB, x, y );
  </code></pre>
  
  <p>This gets a compiler error converting foo.MethodA and foo.MethodB into System.Delegate.  I came up with the workaround below (and I actually like it better because then I get type checking on my arguments to my methods), but I'm curious if there's a way to do what I was originally trying to do?  I know I could use <code>foo.GetType().GetMethod("MethodA")</code> and invoke that, but I was trying to avoid reflection.  I mainly just want to understand how methods are dynamically invoked in .net.</p>
  
  <p>Workaround:</p>
  
  <pre><code>private delegate void EmptyDelegate();
  
  private void InvokeHelper(EmptyDelegate method)
  {
    bool retry = false;
  
    do
    {
      try
      {
        method.Invoke();
        retry = false;
      }
      catch (MyException ex)
      {
        retry = HandleException(ex);
      }
    } while (retry);
  }
  </code></pre>
  
  <p>then call:</p>
  
  <pre><code>InvokeHelper(delegate() { foo.MethodA(a, b, c); });
  InvokeHelper(delegate() { foo.MethodB(x, y); });
  </code></pre>