public static object CompileAndExecuteCode(Window window, string formulaCode, out bool isError) { isError = false; try { Microsoft.CodeAnalysis.Scripting.Script <object> script = CSharpScript.Create(formulaCode, ScriptOptions.Default. WithImports("System", "System.Collections", "OfficeOpenXml", "System.IO"). WithReferences(typeof(ExcelPackage).Assembly)); Microsoft.CodeAnalysis.Scripting.ScriptState <object> result = script.RunAsync().Result; return(result.ReturnValue); } catch (CompilationErrorException ex) { string errors = ex.Diagnostics.Select(d => d.GetMessage()).First().ToString(); isError = true; MessageBox.Show(window, errors, string.Empty, MessageBoxButton.OK); return(null); } }
public Script(string path, T api) { this.api = api; script = CSharpScript.Create(File.ReadAllText(path), globalsType: typeof(T)); }