有没有办法从C#调用Python代码,我假设使用IronPython?如果是这样,怎么样?
Jef*_*ado 113
这个过程很简单,特别是在C#/ .NET 4应用程序中,通过使用dynamic类型来改进对动态语言的支持.但这最终取决于您打算如何在应用程序中使用(Iron)Python代码.您可以始终ipy.exe作为单独的进程运行并传递源文件,以便它们可以执行.但是您可能希望在C#应用程序中托管它们.这给你留下了很多选择.
添加对IronPython.dll和Microsoft.Scripting.dll程序集的引用.您通常会在根IronPython安装目录中找到它们.
添加using IronPython.Hosting;到源代码的顶部并使用创建IronPython脚本引擎的实例Python.CreateEngine().
你有几个选项,但基本上你创建一个ScriptScope或ScriptSource存储它作为一个dynamic变量.如果您选择这样做,这允许您执行它或从C#操作范围.
使用CreateScope()创建一个空的ScriptScope直接在C#代码的使用,但可使用在Python源.您可以将这些视为解释器实例中的全局变量.
dynamic scope = engine.CreateScope();
scope.Add = new Func<int, int, int>((x, y) => x + y);
Console.WriteLine(scope.Add(2, 3)); // prints 5
Run Code Online (Sandbox Code Playgroud)
使用Execute()一个字符串来执行任意代码的IronPython.您可以使用重载来传入a ScriptScope来存储或使用代码中定义的变量.
var theScript = @"def PrintMessage():
print 'This is a message!'
PrintMessage()
";
// execute the script
engine.Execute(theScript);
// execute and store variables in scope
engine.Execute(@"print Add(2, 3)", scope);
// uses the `Add()` function as defined earlier in the scope
Run Code Online (Sandbox Code Playgroud)
使用ExecuteFile()以执行IronPython的源文件.您可以使用重载来传入a ScriptScope来存储或使用代码中定义的变量.
// execute the script
engine.ExecuteFile(@"C:\path\to\script.py");
// execute and store variables in scope
engine.ExecuteFile(@"C:\path\to\script.py", scope);
// variables and functions defined in the scrip are added to the scope
scope.SomeFunction();
Run Code Online (Sandbox Code Playgroud)
使用GetBuiltinModule()或ImportModule()扩展方法创建包含所述模块中定义的变量的范围.必须在搜索路径中设置以这种方式导入的模块.
dynamic builtin = engine.GetBuiltinModule();
// you can store variables if you want
dynamic list = builtin.list;
dynamic itertools = engine.ImportModule("itertools");
var numbers = new[] { 1, 1, 2, 3, 6, 2, 2 };
Console.WriteLine(builtin.str(list(itertools.chain(numbers, "foobar"))));
// prints `[1, 1, 2, 3, 6, 2, 2, 'f', 'o', 'o', 'b', 'a', 'r']`
// to add to the search paths
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:\path\to\modules");
engine.SetSearchPaths(searchPaths);
// import the module
dynamic myModule = engine.ImportModule("mymodule");
Run Code Online (Sandbox Code Playgroud)
您可以在.NET项目中托管Python代码.C#有助于弥合差距更容易处理.结合这里提到的所有选项,您可以做任何你想要的事情.当然,您可以使用IronPython.Hosting命名空间中的类来执行更多操作,但这应该足以让您入门.
LiR*_*RoN 10
要运行一个函数,您不能像 Jeff Mercado 的响应中的选项 3 那样调用它(这是一个很棒且非常有用的方法!但此选项无法编译,至少在 .NET 4.5 上)。您可以使用 ScriptScope.GetVariable 来获取实际函数,然后您可以像调用 C# 函数一样调用它。像这样使用它:
C#代码:
var var1,var2=...
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
engine.ExecuteFile(@"C:\test.py", scope);
dynamic testFunction = scope.GetVariable("test_func");
var result = testFunction(var1,var2);
Run Code Online (Sandbox Code Playgroud)
蟒蛇代码:
def test_func(var1,var2):
...do something...
Run Code Online (Sandbox Code Playgroud)
我花了一段时间才最终弄明白,这很简单..太糟糕了,没有好的 IronPython 文档。希望这可以帮助 :)
小智 6
1)需要安装
Install-Package DynamicLanguageRuntime -Version 1.2.2
Run Code Online (Sandbox Code Playgroud)
2)需要使用这个添加“Iropython.dll”:https ://www.webucator.com/how-to/how-add-references-your-visual-studio-project.cfm
3) 需要使用
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting;
Run Code Online (Sandbox Code Playgroud)
3)需要设置var
ScriptEngine engine = Python.CreateEngine();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40710 次 |
| 最近记录: |