2
我建立一個CodeCompileUnit和下面的代碼輸出C#源代碼文件,一個.dll,我可以從組件獲得一個實例:CompileAssemblyFromDom拋出FileNotFoundException異常
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]);
到目前爲止好。現在我想避免生成這些文件:
CompilerParameters cp = new CompilerParameters { GenerateInMemory = true, GenerateExecutable = false};
cp.ReferencedAssemblies.Add("MyProgram.exe");
//cp.OutputAssembly = filePath + ".dll";
CompileResults cr = provider.CompileAssemblyFromDom(cp, compileUnit);
這引發FileNotFoundException。它在我的\ temp文件夾中查找x32savit.dll(或類似的),但它不在那裏。如果我取消註釋OutputAssembly,它將失敗,但在該路徑上。