简体   繁体   中英

How do I grab what is written to console with CSharpCodeProvider

I use following to compile C# in runtime:

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; I wanna grab what is written.

You also should check 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. Check: cr.NativeCompilerReturnValue.ToString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM