RickStrahl/Westwind.Scripting

Redirecting Standard Output

SteveOhByte opened this issue · 1 comments

I'm curious how Console.WriteLine can be used when using this:

CSharpScriptExecution script = new() { SaveGeneratedCode = true };
script.AddAssembly(Utils.assembly); // Reference this assembly in the script
script.AddDefaultReferencesAndNamespaces();
            
// need dynamic since current app doesn't know about type
dynamic compiledClass = script.CompileClass(code);

My compiled class is running, I know that much from fetching simple variables within it. But the methods that fetch variables are supposed to send a Console.WriteLine message letting me know they're all clear. Is there a way to capture / redirect these messages from the compiled code so that they will perform the same as output messages in the regular code file?

That depends on what you're hosting your code in. If the Console is available you can write to it from the dynamic code - if not, not so much. Anything you can do in 'normal' code you should be able to do in the dynamically compiled code given it's running in the same environment.

In Markdown Monster (a desktop app) I do something like this where there is no console by building a pseudo console as a streaming string area, but it's basically built by hand and you have to manage that warts and all.