public async Task ExecuteTest() { DumpOutHelper.OutputAction += Output; try { await _scriptEngine.Execute("\"Hello NatashaPad\".Dump();", new NScriptOptions()); await _scriptEngine.Execute("Console.WriteLine(\"Hello NatashaPad\");", new NScriptOptions()); } catch (Natasha.Error.CompilationException ex) { _testOutputHelper.WriteLine(ex.Diagnostics.Select(d => d.ToString()).StringJoin(Environment.NewLine)); throw; } finally { DumpOutHelper.OutputAction -= Output; } }
private async void BtnRun_OnClick(object sender, RoutedEventArgs e) { var input = txtInput.Text?.Trim(); if (string.IsNullOrEmpty(input)) { return; } txtOutput.Text = string.Empty; try { if (input.Contains("static void Main(") || input.EndsWith(";")) { // statements, execute await _scriptEngine.Execute(input, _scriptOptions); } else { // expression, eval var result = await _scriptEngine.Eval(txtInput.Text, _scriptOptions); if (null == result) { txtOutput.AppendText("(null)"); } else { var dumpedResult = _dumperResolver.Resolve(result.GetType()) .Dump(result); txtOutput.AppendText(dumpedResult); } } } catch (Exception exception) { MessageBox.Show(exception.Message, "执行发生异常"); } }