Guest User

Untitled

a guest
Nov 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public static async Task<FormulaEvalException> TryEvalAsync<T>(this T formulaContext) where T : FormulaContext
  2. {
  3. FormulaEvalException res = null;
  4. ScriptState state = null;
  5. var scriptOptions = ScriptOptions.Default.WithReferences("System", "System.Linq", "System.Globalization", "Microsoft.CSharp").WithImports(new[] { "System", "System.Linq", "System.Math", "System.Globalization", "System.Collections.Generic" });
  6. foreach (var formulaList in formulaContext.AllFormulas.Values)
  7. {
  8. foreach (var formula in formulaList)
  9. {
  10. formulaContext.CurrentFormula = formula;
  11. try
  12. {
  13. if (state == null)
  14. {
  15. state = await CSharpScript.RunAsync(formula.Script, scriptOptions, formulaContext);
  16. }
  17. else
  18. {
  19. state = await state.ContinueWithAsync(formula.Script);
  20. }
  21. var result = state.ReturnValue;
  22. if (result == null)
  23. {
  24. if (res == null)
  25. {
  26. res = new FormulaEvalException(formula.Title + " : No result");
  27. }
  28. continue;
  29. }
  30.  
  31. formula.Result = result;
  32. }
  33. catch (CompilationErrorException ex)
  34. {
  35. if (res == null)
  36. {
  37. res = new FormulaEvalException(formula.Title + ex.Message);
  38. }
  39. continue;
  40. }
  41. catch
  42. {
  43. continue;
  44. }
  45. }
  46. }
  47.  
  48. return res;
  49. }
Add Comment
Please, Sign In to add comment