[英]How do I grab what is written to console with CSharpCodeProvider
I use following to compile C# in runtime: 我使用以下代码在运行时编译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
is an empty list, and If the code has Console.WriteLine()
, It gets written to main application's console, however; res.Output
是一个空列表,如果代码具有Console.WriteLine()
,则会将其写入主应用程序的控制台; I wanna grab what is written. 我想抓住写的东西。
You also should check res.Errors
. 您还应该检查
res.Errors
。 If there are errors, then they will be there instead. 如果有错误,那么它们将会在那里。 If both
Errors
and Output
are empty, then you may have had a successful compilation without any output. 如果“
Errors
和“ Output
均为空,则可能已成功编译而没有任何输出。 Check: cr.NativeCompilerReturnValue.ToString()
检查:
cr.NativeCompilerReturnValue.ToString()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.