title: Compile at runtime with referenced assemblies
body: <p>I have created a function to compile C# code in a string in memory and use it at runtime. It works very well.</p>
  
  <p>Next I created a small class library (lets call it mynew.dll) and placed it at c:\mylibraries\mynew.dll.</p>
  
  <p>In my code I can add a referenced assembly. I do it like this:</p>
  
  <pre><code>CSharpCodeProvider provider = new CSharpCodeProvider();
  CompilerParameters parameters = new CompilerParameters
  {
      GenerateExecutable = false,
      GenerateInMemory = true
  };
  
  parameters.ReferencedAssemblies.Add(@"c:\mylibraries\mynew.dll");
  
  CompilerResults results = provider.CompileAssemblyFromSource(parameters, mycode);
  </code></pre>
  
  <p>Now, in my C# code-string (mycode) is a function called "Execute". When I declare the namespace and class from the mynew.dll in this function I get this error:    </p>
  
  <blockquote>
    <p>Could not load file or assembly 'MyNew, Version=1.0.0.0,
    Culture=neutral, PublicKeyToken=null' or one of its dependencies. The
    system cannot find the file specified.</p>
  </blockquote>
  
  <p>When I copy the dll in the bin/debug folder of the main application is suddenly works, but I don't want this. I want the code, that is compiled at runtime, to use it, not the main application.</p>
  
  <p>This I did before posting here:</p>
  
  <ul>
  <li>Search Stackoverflow </li>
  <li>Used Google </li>
  <li>Tried to compile the dll to the disk and not in memory. This works... Problem now is that the dll is locked in IIS and I cannot delete it without restarting the IIS server; no option since the code can change any moment and I don't want to restart the server when people are on the website.</li>
  </ul>
  
  <p>Is there a way to fix this?</p>