使用.NET?将多个代码文件编译为程序集

本文关键字:文件 编译 程序集 代码 NET 使用 | 更新日期: 2023-09-27 17:57:59

string code = System.IO.File.ReadAllText(path);
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = false;
options.GenerateInMemory = true;
options.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
CompilerResults result;
result = codeProvider.CompileAssemblyFromSource(options, code);
LastErrors = result.Errors;
if (result.Errors.HasErrors)
{
    throw new Exception("Compiling the code has returned exceptions!'r'nCheck LastErrors for details.");
}
return result.CompiledAssembly;

是我将C#代码编译成程序集的代码
但是,我希望以某种方式将目录中的所有文件编译到该程序集中。

使用.NET?将多个代码文件编译为程序集

您可以不直接使用CompileAssemblyFromFile吗?这允许您指定多个文件名。