以前听说过.net可以执行python代码,这次终于派上用场了
NUGET 安装 IronPython .NET版本4.8以下选择 2.7.8 这个版本
public static string ExecPython(string pyCode)
{
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
var sourceCode = engine.CreateScriptSourceFromString(pyCode);
var result = sourceCode.Execute<object>(scope);
return result.ToString();
}
关键词需要符合python的语法规范
对于简单表达式,自带的DataTable也能解析,测试无法判断==
但是速度比py的快
string expression = "1+1";
DataTable eval = new DataTable();
object result = eval.Compute(expression, "");
Console.WriteLine(result);
原文:https://www.cnblogs.com/trykle/p/13321509.html