ILParser.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #if REFLECTION_EMIT_COMPILER
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Reflection;
  7. namespace FastReport.Code.Expressions
  8. {
  9. public abstract class ILParser
  10. {
  11. private static readonly ConcurrentDictionary<string, MethodInfo> _cache = new ConcurrentDictionary<string, MethodInfo>();
  12. public static ILParser Create(Report report)
  13. {
  14. return new CSharpILParser(report);
  15. }
  16. private static object Invoke(MethodInfo method, object[] args)
  17. {
  18. var result = method.Invoke(null, args);
  19. //Debug.WriteLine(result);
  20. return result;
  21. }
  22. public object StartDynamic(string expression, Report report, Variant value)
  23. {
  24. if (!_cache.TryGetValue(expression, out var compiledExpression))
  25. {
  26. Debug.WriteLine($"Compilation: {expression}");
  27. compiledExpression = CompileExpression(expression, new[] {report.GetType(), value.GetType()});
  28. _cache.TryAdd(expression, compiledExpression);
  29. }
  30. return Invoke(compiledExpression, new object[] { report, value });
  31. }
  32. protected abstract MethodInfo CompileExpression(string expression, Type[] argTypes);
  33. }
  34. }
  35. #endif