C# 是否可以在.NET应用程序中运行python?
NET应用程序可以将C#代码以字符串形式保存在文本文件或数据库中,并动态运行。该方法在许多情况下都很有用,例如业务规则引擎或用户定义的计算引擎等。 下面是一个很好的例子:C# 是否可以在.NET应用程序中运行python?,c#,python,.net,dynamic,ironpython,C#,Python,.net,Dynamic,Ironpython,NET应用程序可以将C#代码以字符串形式保存在文本文件或数据库中,并动态运行。该方法在许多情况下都很有用,例如业务规则引擎或用户定义的计算引擎等。 下面是一个很好的例子: using System; using System.Collections.Generic; using System.Linq; using Microsoft.CSharp; using System.CodeDom.Compiler; class Program { static void Main(stri
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
class Program
{
static void Main(string[] args)
{
var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
parameters.GenerateExecutable = true;
CompilerResults results = csc.CompileAssemblyFromSource(parameters,
@"using System.Linq;
class Program {
public static void Main(string[] args) {
var q = from i in Enumerable.Range(1,100)
where i % 2 == 0
select i;
}
}");
results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
}
}
是Python编程语言在.NET(C#)中的一个实现。在.NET版本4.0之后,IronPython的代码可以在DLR(动态语言运行时)的帮助下嵌入到.NET应用程序中
下面是一个关于如何嵌入它的非常合理的最新示例:
您还可以阅读及其以获取有关该主题的其他信息
谷歌也提供了很多关于谷歌的教程
希望这有帮助
IronPython是Python编程语言的开源实现,它与.NET框架紧密集成。IronPython可以使用.NET框架和Python库,而其他.NET语言也可以轻松地使用Python代码
从…起
在他们的站点中有一些工具可以与VisualStudio集成。
如果您的目标是在运行时使用反射和编译python代码(如使用CompileAseMblyFromSource方法),您可以看到:
你听说过IronPython吗?你能跟我分享更多细节吗,我只是在nuget上看到的。我还编辑了这个问题,将IronPython作为标记,因为这可能会带来更有用的答案:)PythonNet也是一个选项。我相信与其他python模块的兼容性更好。@M4rtini是否可以发布简单的示例?
class Program
{
static void Main(string[] args)
{
var pythonCode = @"
a=1
b=2
c=a+b
return c";
//how to execute python code in c# .net
}
}