简体   繁体   English

如何使用CSharpCodeProvider获取写到控制台的内容

[英]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.

相关问题 如何使用CSharpCodeProvider类运行C#4.0编译器? - How do I run C# 4.0 compiler with CSharpCodeProvider class? 如果我没有使用CSharpCodeProvider指定CompilerVersion,为什么大多数样本都指定它,会发生什么? - What happens if I don't specify CompilerVersion with CSharpCodeProvider and why do most samples specify it? CSharpCodeProvider似乎陷入了.NET 2.0,我如何获得新功能? - CSharpCodeProvider seems to be stuck at .NET 2.0, how do I get new features? Mono用什么语言编写,如何阅读源代码? - What language is Mono written in and how do I read the source code? 如何找到已经写在控制台上的特定角色并在其中设置光标位置? C# - How do I find a specific caracter already written on the console and set the cursor position there? C# 如何抓取所有子游戏对象? - How do I grab all sub gameobjects? 如何使用 CSharpCodeProvider 传递变量? - How to pass a variable with CSharpCodeProvider? 如何重新分配标准错误流,以便库代码抛出的异常也会写入文件而不是控制台? - How do I re-assign the standard error stream such that exceptions thrown by library code also get written to file instead of the console? CSharpCodeProvider输出控制台应用程序作为Windows窗体应用程序 - CSharpCodeProvider Output console app as windows form app 如何卸载由 CSharpCodeProvider 创建的程序集? - How to unload assembly created by CSharpCodeProvider?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM