title: Create a new method at run-time
body: <p>I want to learn the neat trick of creating a method on the fly with a signature that some client code wants. Just like <code>System.Delegate.BeginInvoke</code>, <code>EndInvoke</code> and <code>Invoke</code> methods.</p>
  
  <p>I am looking inside the <code>System.Delegate</code> class for these methods but I can't find them (except for <code>DynamicInvoke</code>, which isn't fun because it dynamically dispatches calls to late bound methods anyway), so obviously these methods are made at run-time.</p>
  
  <p>I'm scratching my head as to how, if I had to implement such a thing, I would do. I am thinking aloud:</p>
  
  <p>1) Code DOM won't work because I am not interested in constructing a new assembly. I just want to add a new method to a class within the same assembly.</p>
  
  <p>2) Good old reflection will work just fine. I can get to creating a new <code>MethodInfo</code> that has the same parameters as the client code specifies, but then how do I get this new <code>MethodInfo</code> thingy to show up in my class as a method?</p>
  
  <p>Can anyone shed light on this?</p>