Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static async Task<FormulaEvalException> TryEvalAsync<T>(this T formulaContext) where T : FormulaContext
- {
- FormulaEvalException res = null;
- ScriptState state = null;
- var scriptOptions = ScriptOptions.Default.WithReferences("System", "System.Linq", "System.Globalization", "Microsoft.CSharp").WithImports(new[] { "System", "System.Linq", "System.Math", "System.Globalization", "System.Collections.Generic" });
- foreach (var formulaList in formulaContext.AllFormulas.Values)
- {
- foreach (var formula in formulaList)
- {
- formulaContext.CurrentFormula = formula;
- try
- {
- if (state == null)
- {
- state = await CSharpScript.RunAsync(formula.Script, scriptOptions, formulaContext);
- }
- else
- {
- state = await state.ContinueWithAsync(formula.Script);
- }
- var result = state.ReturnValue;
- if (result == null)
- {
- if (res == null)
- {
- res = new FormulaEvalException(formula.Title + " : No result");
- }
- continue;
- }
- formula.Result = result;
- }
- catch (CompilationErrorException ex)
- {
- if (res == null)
- {
- res = new FormulaEvalException(formula.Title + ex.Message);
- }
- continue;
- }
- catch
- {
- continue;
- }
- }
- }
- return res;
- }
Add Comment
Please, Sign In to add comment