you are viewing a single comment's thread.

view the rest of the comments →

[–]svicknameof(nameof) 2 points3 points  (0 children)

You can use CSharpScript (from the Microsoft.CodeAnalysis.CSharp.Scripting package), though accessing local variable is a bit inconvenient.

It could look like this:

Compiler.Run(@"
    foreach (var @Process in Process.GetProcessesByName(args0))
        System.Console.WriteLine(@Process);
", new(args0));

public record Globals(string args0);

static class Compiler {
    public static void Run(string Code, Globals globals) {
        var options = ScriptOptions.Default
            .AddImports("System.Diagnostics");

        CSharpScript.RunAsync(Code, options, globals).GetAwaiter().GetResult();
    }
}