samedi 20 juillet 2019

Performance issue with compilation using CSharpCodeProvider

I have an application where user can create its own formula it works fine

for all case but when i have Date Time Fields in my formula the compilation part of the code take around 132 ms for one formula where at the same time i have 31 formula each with different value.

enter code here

`using System;
 namespace abc
 {
   public class xyz
   {
     var code = @"
            using System;
            namespace First
           {
             public class program
              {
                   public static object main()
              {
               if(Convert.ToDateTime(\"01-04-2019 
               10:25:00\")>Convert.ToDateTime(\"01-04-2019 15:00:00\"))
              {
                  return 1;
              }
              else
              {
                 return 0;
              }
          }
         }
     }
    ";`

  `var options = new CompilerParameters();
  options.GenerateExecutable = false;
  options.GenerateInMemory = false;`

  `var provider = new CSharpCodeProvider();
   var compile = provider.CompileAssemblyFromSource(options, code);`

  `var type = compile.CompiledAssembly.GetType("Abc");
  var abc = Activator.CreateInstance(type);`

  `var method = type.GetMethod("Get");
  var result = method.Invoke(abc, null);`

  `Console.WriteLine(result); //output: abc`
  `}`
  `}`

At this point var compile = provider.CompileAssemblyFromSource(options, code); It takes 132 ms but if don't use any date fields it takes 1 ms





Aucun commentaire:

Enregistrer un commentaire