Loading Custom Assemblies with CompileAssemblyFromSource
c#
Solution
This is because the compilation step failed and you have not check it for errors...
static Assembly Compile(string script)
{
CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = false;
options.GenerateInMemory = true;
options.ReferencedAssemblies.Add("System.dll");
options.ReferencedAssemblies.Add("ScriptProxy.dll");
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
CompilerResults result = provider.CompileAssemblyFromSource(options, script);
// Check the compiler results for errors
StringWriter sw = new StringWriter();
foreach (CompilerError ce in result.Errors)
{
if (ce.IsWarning) continue;
sw.WriteLine("{0}({1},{2}: error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText);
}
// If there were errors, raise an exception...
string errorText = sw.ToString();
if (errorText.Length > 0)
throw new ApplicationException(errorText);
return result.CompiledAssembly;
}
Problem
I'm not quite sure where to go with this. The overall objective is to be able to take a user script, and execute it within a .NET environment. I have most of the code written and things work provided I don't attempt to load my own assemblies. However, to safely give users access to internal portions of the system, a proxy DLL has been created. This is where the problem seems to be. Right now this proxy DLL has one thing in it, an interface. ``` CompilerParameters options = new CompilerParameters(); options.GenerateExecutable = false; options.GenerateInMemory = true; options.ReferencedAssemblies.Add("System.dll"); options.ReferencedAssemblies.Add("ScriptProxy.dll"); Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); CompilerResults result = provider.CompileAssemblyFromSource(options, script); // This line here throws the error: return result.CompiledAssembly; ``` Running the above code, it throws the following error: System.IO.FileNotFoundException : Could not load file or assembly 'file:///C:\Users...\AppData\Local\Temp\scts5w5o.dll' or one of its dependencies. The system cannot find the file specified. Of course my first thought is, "...and what is scts5w5o.dll?" Is this the `ScriptProxy.dll` not loading properly, or is the ScriptProxy.dll itself trying to load up dependencies, which are in a temp file somewhere? Or is it something completely different? I should mention, I'm executing this code from the NUnit test runner. I'm not sure if that makes a difference.