日期:2014-05-16 浏览次数:20899 次
public int FindParamer(string strParamer)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameter = new CompilerParameters();
parameter.ReferencedAssemblies.Add("System.dll");
parameter.GenerateExecutable = false;
parameter.GenerateInMemory = true;
CompilerResults result =
provider.CompileAssemblyFromSource
(
parameter,
"using System;" +
"namespace calculate" +
"{" +
"public static class calculation" +
"{" +
"public static int calculateExpression()" +
"{" +
"return " + strParamer +
";}}}"
);
return Convert.ToInt32(result.CompiledAssembly.GetType("calculate.calculation").GetMethod("calculateExpression").Invoke(null,new object[]{}));
}