【问题标题】:CompileAssemblyFromDom throws FileNotFoundExceptionCompileAssemblyFromDom 抛出 FileNotFoundException
【发布时间】:2012-06-19 17:23:55
【问题描述】:

我构建了一个 CodeCompileUnit,下面的代码输出了一个 C# 源代码文件,一个 .dll,我可以从程序集中获取一个实例:

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,它会失败,但在该路径上。

【问题讨论】:

    标签: c# .net windows visual-studio-2010 codedom


    【解决方案1】:

    原来是命名空间的错误。有一个很好的例子here

    添加以下代码在调试时非常有帮助。

      string errorText = String.Empty;
      foreach (CompilerError compilerError in compilerResults.Errors)
          errorText += compilerError + "\n";
    

    【讨论】:

      猜你喜欢
      • 2013-05-24
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多