title: How to create a delegate for a reflected method at run time
body: <p>I want to create a <code>Delegate</code> of a reflected method, but the <a href="http://msdn.microsoft.com/en-us/library/system.delegate.createdelegate.aspx" rel="nofollow noreferrer"><code>Delegate.CreateDelegate</code></a> requires the <code>Type</code> of delegate be specified. Is it possible to dynamically create a 'Delegate' that matches whatever function is reflected?</p>
  
  <p>Here is a simple example:</p>
  
  <pre><code>class Functions
  {
      public Functions()
      {
  
      }
  
      public double GPZeroParam()
      {
          return 0.0;
      }
  
      public double GPOneParam(double paramOne)
      {
          return paramOne;
      }
  
      public double GPTwoParam(double paramOne, double paramTwo)
      {
          return paramOne+paramTwo;
      }
  }
  
  static void Main(string[] args)
  {
      Dictionary&lt;int, List&lt;Delegate&gt;&gt; reflectedDelegates = new Dictionary&lt;int, List&lt;Delegate&gt;&gt;();
      Functions fn = new Functions();
      Type typeFn = fn.GetType();
      MethodInfo[] methods = typeFn.GetMethods();
  
      foreach (MethodInfo method in methods)
      {
          if (method.Name.StartsWith("GP"))
          {
              ParameterInfo[] pi = method.GetParameters();
  
              if (!reflectedDelegates.ContainsKey(pi.Length))
              {
                  reflectedDelegates.Add(pi.Length, new List&lt;Delegate&gt;());
              }
  
              // How can I define a delegate type for the reflected method at run time?
              Delegate dlg = Delegate.CreateDelegate(typeof(???), fn, method);
              reflectedDelegates[pi.Length].Add(dlg);
          }
      }
  }
  </code></pre>
  
  <h2>Update:</h2>
  
  <p>The closest thing I found is this <a href="http://www.codeproject.com/KB/cs/FastInvokerWrapper.aspx" rel="nofollow noreferrer">FastInvokeWrapper</a> on code-project, but I'm still trying to wrap my head around it and I don't quite understand how <code>GetMethodInvoker</code> binds the reflected method to the <code>FastInvokeHandler</code>.</p>