private async void btnEval_Click(object sender, EventArgs e) { txtResult.Text = ""; var options = ScriptOptions.Default .WithImports( "System", "System.Collections", "System.Collections.Generic", "System.Threading.Tasks" ); var n = _args.Length; var args = new object[n]; for (var i = 0; i < n; i++) { var arg = _args[i]; var type = arg.Type; var value = $"({ToCSharp(type)})({arg.Value})"; try { args[i] = await CSharpScript.EvaluateAsync(value, options); } catch (Exception ex) { MessageBox.Show($"Error when compiling value for parameter '{arg.Parameter}':\r\n\r\n{ex.Message}", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } var f = default(Delegate); try { f = _function.Compile(); } catch (Exception ex) { MessageBox.Show($"Error when compiling expression:\r\n\r\n{ex.Message}", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var res = default(object); var cout = new StringWriter(); var dbg = new DebugWriter(cout, AppendLive); try { Console.SetOut(dbg); res = f.DynamicInvoke(args); } catch (TargetInvocationException ex) { txtResult.ForeColor = Color.Red; txtResult.Text = ex.InnerException.Message; _done = true; tabControl1.SelectedTab = tabResult; txtResult.SelectionStart = txtResult.SelectionStart = 0; txtResult.ScrollToCaret(); return; } catch (Exception ex) { MessageBox.Show($"Error when evaluating expression:\r\n\r\n{ex.Message}", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var task = res as Task; if (task != null) { txtResult.Text = "Awaiting task..."; try { await task; var txt = ObjectDumper.Write(res); txtResult.Text = "Awaiting task... Done!\r\n" + txt + "\r\n\r\nConsole output:\r\n" + cout.ToString(); _done = true; tabControl1.SelectedTab = tabResult; txtResult.SelectionStart = txtResult.SelectionStart = 0; txtResult.ScrollToCaret(); } catch (Exception ex) { MessageBox.Show($"Error when evaluating expression:\r\n\r\n{ex.Message}", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { var txt = ObjectDumper.Write(res); txtResult.ForeColor = Color.Black; txtResult.Text = txt + "\r\n\r\nConsole output:\r\n" + cout.ToString(); _done = true; tabControl1.SelectedTab = tabResult; txtResult.SelectionStart = txtResult.SelectionStart = 0; txtResult.ScrollToCaret(); } }