title: Recompile C# while running, without AppDomains
body: <p>Let’s say that I have two C# applications - <code>game.exe</code> (XNA, needs to support Xbox 360) and <code>editor.exe</code> (XNA hosted in WinForms) - they both share an <code>engine.dll</code> assembly that does the vast majority of the work.</p>
  
  <p>Now let’s say that I want to add some kind of C#-based scripting (it’s not quite "scripting" but I’ll call it that). Each level gets its own class inherited from a base class (we’ll call it <code>LevelController</code>).</p>
  
  <p>These are the important constraints for these scripts:</p>
  
  <ol>
  <li><p>They need to be real, compiled C# code</p></li>
  <li><p>They should require minimal manual "glue" work, if any</p></li>
  <li><p>They must run in the same AppDomain as everything else</p></li>
  </ol>
  
  <p>For the game - this is pretty straight forward: All the script classes can be compiled into an assembly (say, <code>levels.dll</code>) and the individual classes can be instanced using reflection as needed. </p>
  
  <p>The editor is much harder. The editor has the ability to "play the game" within the editor window, and then reset everything back to where it started (which is why the editor needs to know about these scripts in the first place).</p>
  
  <p>What I am trying to achieve is basically a "reload script" button in the editor that will recompile and load the script class associated with the level being edited and, when the user presses the "play" button, create an instance of the most recently compiled script.</p>
  
  <p>The upshot of which will be a rapid edit-test workflow within the editor (instead of the alternative - which is to save the level, close the editor, recompile the solution, launch the editor, load the level, test).</p>
  
  <hr>
  
  <p>Now I <em>think</em> I have worked out a potential way to achieve this - which itself leads to a number of questions (given below):</p>
  
  <ol>
  <li><p>Compile the collection of <code>.cs</code> files required for a given level (or, if need be, the whole <code>levels.dll</code> project) into a temporary, unique-named assembly. That assembly will need to reference <code>engine.dll</code>. How to invoke the compiler this way at runtime? How to get it to output such an assembly (and can I do it in memory)? </p></li>
  <li><p>Load the new assembly. Will it matter that I am loading classes with the same name into the same process? (I am under the impression that the names are qualified by assembly name?)</p>
  
  <p>Now, as I mentioned, I can’t use AppDomains. But, on the other hand, I don’t mind leaking old versions of script classes, so the ability to unload isn’t important. Unless it is? I’m assuming that loading maybe a few hundred assemblies is feasible.</p></li>
  <li><p>When playing the level, instance the class that is inherited from LevelController from the specific assembly that was just loaded. How to do this?</p></li>
  </ol>
  
  <p>And finally:</p>
  
  <p>Is this a sensible approach? Could it be done a better way?</p>
  
  <hr>
  
  <p>UPDATE: These days I use a <a href="https://stackoverflow.com/a/10192314/165500">far simpler approach</a> to solve the underlying problem.</p>