ExpressionCompiler.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Microsoft.CSharp;
  2. using System;
  3. using System.CodeDom.Compiler;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using GDNXFD.Data;
  10. namespace GDNXFD.Alert.Interpreter
  11. {
  12. public class ExpressionCompiler
  13. {
  14. public static ExpressionCompilerResults CompileExpression(AlertRule alertRule)
  15. {
  16. if (alertRule == null)
  17. return null;
  18. CodeDomProvider cpd = new CSharpCodeProvider();
  19. CompilerParameters cp = new CompilerParameters();
  20. cp.ReferencedAssemblies.Add("System.dll");
  21. cp.ReferencedAssemblies.Add("GDNXFD.Alert.Interpreter.dll");
  22. cp.ReferencedAssemblies.Add("GDNXFD.Data.dll");
  23. cp.GenerateExecutable = false;
  24. CodeGenerator cGen = new CodeGenerator(alertRule.Id, alertRule.Expression);
  25. string sourceCode = cGen.ToClassString();
  26. CompilerResults cr = cpd.CompileAssemblyFromSource(cp, sourceCode);
  27. return new ExpressionCompilerResults(cr, alertRule);
  28. }
  29. }
  30. }