我有一个应用程序,用户可以在其中创建自己的公式,效果很好 对于所有情况,但是当我的公式中有DateTime字段时,一个公式的代码编译部分大约需要132毫秒,而同时我 有31个公式,每个公式具有不同的值。
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
namespace abc
{
public class Abc
{
static void Main()
{
var code = @"
using System;
namespace First
{
public class program
{
public static int Main()
{
if(Convert.ToDateTime(""01-04-2019 10:25:00"")>Convert.ToDateTime(""01-04-2019 15:00:00""))
{
return 1;
}
else
{
return 0;
}
}
}
}";
Console.WriteLine(code);
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("First.program");
var abc = Activator.CreateInstance(type);
var method = type.GetMethod("Main");
var result = method.Invoke(abc, null);
Console.WriteLine(result); //output: abc
}
}
}
此时 var compile = provider.CompileAssemblyFromSource(options,code); 它需要132毫秒,但如果不使用任何日期字段,则需要1毫秒