2011-05-29 55 views
2
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將多個代碼文件編譯爲程序集?

回答

6

你能不能只用CompileAssemblyFromFile來代替?這使您可以指定多個文件名。

+0

哇。我很害怕觸摸這些方法,所以我不打算尋找這個方法。謝謝!的xD – Vercas 2011-05-29 11:59:30

相關問題