title: Linking a .NET Expression Tree into a new assembly
body: <p>I'm trying to write my own toy My Toy Language -> MSIL compiler in order to get a better understanding of how compilers work. I got the parsing and lexing working, I have built the expression trees and using the System.Linq.Expressions expression tree API, I have a working interpreter. Now I would like to emit some real MSIL assemblies.</p>
  
  <p>The problem is, I can't figure out how to actually build these assemblies. The <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.methodbuilder.aspx" rel="noreferrer">MethodBuilder</a> class only accepts raw MSIL method bodies, so I have to get the raw MSIL of my expression tree. Calling <a href="http://msdn.microsoft.com/en-us/library/bb345362.aspx" rel="noreferrer">Expression.Compile()</a> returns a working delegate but I'm not able to get its underlying MSIL. Calling <a href="http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getmethodbody.aspx" rel="noreferrer">MethodInfo.GetMethodBody()</a> throws an InvalidOperationException since it's not implemented in that specific child class.</p>
  
  <p>How can I link that delegate into a new assembly?</p>