[英]How do I grab what is written to console with CSharpCodeProvider
我使用以下代碼在運行時編譯C#:
CompilerParameters parameters = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = true,
IncludeDebugInformation = true
};
// True - memory generation, false - external file generation
// True - exe file generation, false - dll file generation
var res = pro.CompileAssemblyFromSource(parameters,
code);
Assembly assembly = res.CompiledAssembly;
Type program = assembly.GetType("First.Program");
MethodInfo main = program.GetMethod("Main");
var invoke = main?.Invoke(null, null);
res.Output
是一個空列表,如果代碼具有Console.WriteLine()
,則會將其寫入主應用程序的控制台; 我想抓住寫的東西。
您還應該檢查res.Errors
。 如果有錯誤,那么它們將會在那里。 如果“ Errors
和“ Output
均為空,則可能已成功編譯而沒有任何輸出。 檢查: cr.NativeCompilerReturnValue.ToString()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.