I build a CodeCompileUnit and the following code outputs a C# source code file, a .dll and I can get an instance from the assembly:
TextWriter tw = new IndentedTextWriter(new StreamWriter(filePath + ".cs", false), " ");
provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions());
tw.Close();
CompilerParameters cp = new CompilerParameters { GenerateInMemory = true, GenerateExecutable = false};
cp.ReferencedAssemblies.Add("MyProgram.exe");
cp.OutputAssembly = filePath + ".dll";
CompilerResults cr = provider.CompileAssemblyFromFile(cp, filePath + ".cs");
MyType instance = (MyType)Activator.CreateInstance(cr.CompiledAssembly.GetTypes()[0]);
So far so good. Now I would like to avoid generating those files:
CompilerParameters cp = new CompilerParameters { GenerateInMemory = true, GenerateExecutable = false};
cp.ReferencedAssemblies.Add("MyProgram.exe");
//cp.OutputAssembly = filePath + ".dll";
CompileResults cr = provider.CompileAssemblyFromDom(cp, compileUnit);
This throws a FileNotFoundException. It looks for a x32savit.dll (or similar) in my \temp folder but it is not there. If I uncomment the OutputAssembly it fails the same but on that path.
It turned out to be an error with namespaces. There is a good example here.
Adding the following code was very helpful while debugging.