Compile multiple code files into an assembly with .NET?
string code = System.IO.File.ReadAllText(path);
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters options = new CompilerParameters();
option开发者_开发百科s.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;
is my code for compiling C# code into an assembly.
But instead, I would like to somehow compile all the files in a directory into that assembly.Can you not just use CompileAssemblyFromFile
instead? That allows you to specify multiple filenames.
精彩评论