public void ExecuteCodeSnippetWithoutResult() { var script = new CSharpScriptExecution() { SaveGeneratedCode = true, }; script.AddDefaultReferencesAndNamespaces(); string result = script.ExecuteCode("Console.WriteLine($\"Time is: {DateTime.Now}\");", null) as string; Console.WriteLine($"Result: {result}"); Console.WriteLine($"Error: {script.Error}"); Console.WriteLine(script.ErrorMessage); Console.WriteLine(script.GeneratedClassCode); Assert.IsFalse(script.Error, script.ErrorMessage); }
/// <summary> /// Evaluates the embedded script parsing out {{ C# Expression }} /// blocks and evaluating the expressions and embedding the string /// output into the result string. /// /// /// </summary> /// <param name="snippet">The snippet template to expand</param> /// <param name="model">Optional model data accessible in Expressions as `Model`</param> /// <returns></returns> public string EvaluateScript(string snippet, object model = null) { #if DEBUG var sw = new Stopwatch(); sw.Start(); #endif var tokens = TokenizeString(ref snippet, "{{", "}}"); snippet = snippet.Replace("\"", "\"\""); snippet = DetokenizeString(snippet, tokens); snippet = snippet.Replace("{{", "\" + ").Replace("}}", " + @\""); snippet = "@\"" + snippet + "\""; string code = "dynamic Model = parameters[0];\r\n" + "return " + snippet + ";"; //var scriptCompiler = new ScriptRunnerRoslyn(); var scriptCompiler = new CSharpScriptExecution() { CompilerMode = ScriptCompilerModes.Roslyn }; scriptCompiler.AddDefaultReferencesAndNamespaces(); scriptCompiler.AddAssemblies("System.dll", "System.Core.dll", "System.Drawing.dll", "Microsoft.CSharp.dll", "System.Data.dll", "MarkdownMonster.exe", "Westwind.Utilities.dll", "System.Configuration.dll", "Newtonsoft.Json.dll"); scriptCompiler.AddNamespaces("System", "System.IO", "System.Reflection", "System.Text", "System.Drawing", "System.Diagnostics", "System.Data", "System.Data.SqlClient", "System.Linq", "System.Collections.Generic", "Newtonsoft.Json", "Newtonsoft.Json.Linq", "MarkdownMonster", "Westwind.Utilities"); string result = scriptCompiler.ExecuteCode(code, model) as string; if (result == null) { ErrorMessage = scriptCompiler.ErrorMessage; } else { ErrorMessage = null; } #if DEBUG sw.Stop(); Debug.WriteLine("ScriptParser Code: \r\n" + code); Debug.WriteLine("Snippet EvaluateScript Execution Time: " + sw.ElapsedMilliseconds + "ms"); #endif return(result); }
public void ExecuteCodeSnippetWithResult() { var script = new CSharpScriptExecution() { SaveGeneratedCode = true, CompilerMode = ScriptCompilerModes.Roslyn, GeneratedNamespace = "ScriptExecutionTesting", GeneratedClassName = "MyTest" }; script.AddDefaultReferencesAndNamespaces(); //script.AddAssembly("Westwind.Utilities.dll"); //script.AddNamespace("Westwind.Utilities"); var code = $@" // Check some C# 6+ lang features var s = new {{ name = ""Rick""}}; // anonymous types Console.WriteLine(s?.name); // null propagation int num1 = (int)parameters[0]; int num2 = (int)parameters[1]; // string templates var result = $""{{num1}} + {{num2}} = {{(num1 + num2)}}""; Console.WriteLine(result); return result; "; string result = script.ExecuteCode(code, 10, 20) as string; Console.WriteLine($"Result: {result}"); Console.WriteLine($"Error: {script.Error}"); Console.WriteLine(script.ErrorMessage); Console.WriteLine(script.GeneratedClassCodeWithLineNumbers); Assert.IsFalse(script.Error, script.ErrorMessage); Assert.IsTrue(result.Contains(" = 30")); result = script.ExecuteCode(code, 15, 10) as string; Console.WriteLine($"Result: {result}"); Console.WriteLine($"Error: {script.Error}"); Console.WriteLine(script.ErrorMessage); Assert.IsFalse(script.Error, script.ErrorMessage); Assert.IsTrue(result.Contains(" = 25")); script = new CSharpScriptExecution() { SaveGeneratedCode = true, GeneratedClassName = "MyTest" }; script.AddDefaultReferencesAndNamespaces(); result = script.ExecuteCode(code, 4, 10) as string; Console.WriteLine($"Result: {result}"); Console.WriteLine($"Error: {script.Error}"); Console.WriteLine(script.ErrorMessage); Assert.IsFalse(script.Error, script.ErrorMessage); Assert.IsTrue(result.Contains(" = 14")); }