I'm trying to dynamically compile code using CSharpCodeProvider. In the referenced assemblies, I'm adding a reference parameter for typeof(Program).Assembly.CodeBase), as was suggested here, but it doesn't work. I still get an error saying
error CS0006: Metadata file 'file:///C:/Code/MyProject/bin/MyProject.DLL' could not be found;
A file by that name does exist - the only difference is that the file extension shows lowercase in file explorer (".dll"), but otherwise, the filename from the error message matches the name and path of the dll I want to reference.
Any idea why the compiler would fail to see the referenced dll in this case?
Here is the relevant section of my code:
CompilerResults result = null;
CompilerParameters parms = new CompilerParameters();
parms.GenerateExecutable = false;
parms.GenerateInMemory = true;
parms.OutputAssembly = "MyOutputAssembly";
parms.ReferencedAssemblies.Add("System.dll");
parms.ReferencedAssemblies.Add("System.Data.dll");
parms.ReferencedAssemblies.Add("mscorlib.dll");
parms.ReferencedAssemblies.Add(typeof(Program).Assembly.CodeBase); // Reference the current assembly
// Lock because CSharpCodeProvider can only compile the code once per time slot
lock (lockCompile)
{
using (CSharpCodeProvider codeProvider = new CSharpCodeProvider())
{
result = codeProvider.CompileAssemblyFromSource(parms, new string[] { code.ToString() });
}
}