【问题标题】:Runtime C# Compile All Files in a Folder from Code运行时 C# 从代码编译文件夹中的所有文件
【发布时间】:2017-09-21 23:42:38
【问题描述】:

我正在使用下面的代码成功地进行运行时编译。除了当输入 *.cs 文件的数量很大时,我得到以下错误。

System.SystemException: 运行 mono.exe 时出错:文件名或扩展名太长。

我认为这是由于this article 中的命令行长度限制。

这里是代码

var codeProvider = new CSharpCodeProvider (
                   new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });

string EngineOutput = Path.GetFileName(dir) + ".dll";

parameters.GenerateExecutable = false;
parameters.OutputAssembly = EngineOutput;

CompilerResults results = codeProvider.CompileAssemblyFromFile (parameters, engineFiles.ToArray ());

由于所有源文件都在一个文件夹中,如果有一种方法可以使用 -recurse 以编程方式传递该文件夹,那就太好了。或者,如果有一种方法可以采用像 codeProvider.CompileAssemblyFromDirectoryRecursive 这样的文件夹,那也很好,但据我所知,这并不存在。

我尝试了以下方法,但没有成功。编译器只选择 fake.cs 而不是 /recurse 指定的文件夹。

parameters.CompilerOptions += " /recurse:" + dir + System.IO.Path.PathSeparator + "*.cs";
results = codeProvider.CompileAssemblyFromFile (parameters, new string[1] { "fake.cs" });

感谢您的提前。

【问题讨论】:

    标签: c# mono csharpcodeprovider runtime-compilation


    【解决方案1】:

    我只是用 DirectorySeparator 替换了 PathSeparator,然后就可以了。

    parameters.CompilerOptions += " /recurse:" + dir + System.IO.Path.DirectorySeparatorChar + "*.cs";
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2017-11-19
      • 2017-02-28
      • 2013-05-27
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多