Report.BaseExt.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using FastReport.Code;
  2. using FastReport.Data;
  3. using FastReport.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. namespace FastReport
  8. {
  9. public partial class Report
  10. {
  11. #if REFLECTION_EMIT_COMPILER
  12. private readonly Dictionary<string, string> _cachedParsedExpressions = new Dictionary<string, string>();
  13. private Code.Expressions.ILParser _ilParser;
  14. private bool IsCompileNeeded { get; set; }
  15. #endif
  16. #region Script related
  17. #if REFLECTION_EMIT_COMPILER
  18. private void SetIsCompileNeeded()
  19. {
  20. // we check report object to AdvMatrix, because this object doesn't work without compilation
  21. if (!scriptChanged)
  22. {
  23. foreach (var reportObject in AllObjects)
  24. {
  25. if (reportObject is ReportComponentBase reportObj)
  26. if (reportObj.IsCompilationNeeded)
  27. {
  28. IsCompileNeeded = true;
  29. break;
  30. }
  31. }
  32. }
  33. else
  34. IsCompileNeeded = scriptChanged;
  35. if (!IsCompileNeeded)
  36. _ilParser = Code.Expressions.ILParser.Create(this);
  37. }
  38. private bool TryReflectionEmit(string expression, Variant value, out object returnValue)
  39. {
  40. returnValue = null;
  41. if (IsCompileNeeded) return false;
  42. try
  43. {
  44. if (!_cachedParsedExpressions.TryGetValue(expression, out var result))
  45. {
  46. result = expression;
  47. // TODO: optimize
  48. if (DataHelper.IsValidColumn(Report.Dictionary, expression))
  49. result = "[" + expression + "]";
  50. result = AssemblyDescriptor.ReplaceDataItems(this, result);
  51. _cachedParsedExpressions.Add(expression, result);
  52. }
  53. returnValue = _ilParser.StartDynamic(result, this, value);
  54. return true;
  55. }
  56. catch (Exception ex)
  57. {
  58. Debug.WriteLine($"ERROR in compilation expression {expression}, message: {ex.Message}");
  59. Config.WriteLogString(ex.Message);
  60. return false;
  61. }
  62. }
  63. #endif
  64. #endregion Script related
  65. }
  66. }