运行时 C# 从代码编译文件夹中的所有文件
Runtime C# Compile All Files in a Folder from Code
我使用下面的代码成功地进行了 运行 时间编译。除了当输入 *.cs 文件的数量很大时,我得到以下错误。
System.SystemException: Error running mono.exe: The filename or extension is too long.
我认为这是由于 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" });
感谢提前。
我只是用 DirectorySeparator 替换了 PathSeparator,然后就可以了。
parameters.CompilerOptions += " /recurse:" + dir + System.IO.Path.DirectorySeparatorChar + "*.cs";
我使用下面的代码成功地进行了 运行 时间编译。除了当输入 *.cs 文件的数量很大时,我得到以下错误。
System.SystemException: Error running mono.exe: The filename or extension is too long.
我认为这是由于 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" });
感谢提前。
我只是用 DirectorySeparator 替换了 PathSeparator,然后就可以了。
parameters.CompilerOptions += " /recurse:" + dir + System.IO.Path.DirectorySeparatorChar + "*.cs";