如何从 C# 中的 Python 脚本调用特定方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13231913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:50:47  来源:igfitidea点击:

How do I call a specific Method from a Python Script in C#?

c#pythonmethodsargumentsironpython

提问by VanDeath

I'm wondering if there is a possibility to call a specific Method from a Python script over a C# project.

我想知道是否有可能通过 C# 项目从 Python 脚本调用特定方法。

I have no code... but my idea is:

我没有代码......但我的想法是:

Python Code:

蟒蛇代码:

def SetHostInfos(Host,IP,Password):
   Work to do...

def CalcAdd(Numb1,Numb2):
   Work to do...

C# Code:

C# 代码:

SetHostInfos("test","0.0.0.0","PWD")
result = CalcAdd(12,13)

How can I call one of the Methods, from this Python script, over C#?

如何通过 C# 从这个 Python 脚本调用其中一种方法?

采纳答案by Simon Opelt

You can host IronPython, execute the script and access the functions defined within the script through the created scope.

您可以托管 IronPython,执行脚本并通过创建的作用域访问脚本中定义的函数。

The following sample shows the basic concept and two ways of using the function from C#.

下面的示例展示了基本概念和使用 C# 函数的两种方法。

var pySrc =
@"def CalcAdd(Numb1, Numb2):
    return Numb1 + Numb2";

// host python and execute script
var engine = IronPython.Hosting.Python.CreateEngine();
var scope = engine.CreateScope();
engine.Execute(pySrc, scope);

// get function and dynamically invoke
var calcAdd = scope.GetVariable("CalcAdd");
var result = calcAdd(34, 8); // returns 42 (Int32)

// get function with a strongly typed signature
var calcAddTyped = scope.GetVariable<Func<decimal, decimal, decimal>>("CalcAdd");
var resultTyped = calcAddTyped(5, 7); // returns 12m

回答by Paul Collingwood

You could make your python program take arguments on the command line then call it as a command line app from your C# code.

你可以让你的 python 程序在命令行上接受参数,然后从你的 C# 代码中调用它作为命令行应用程序。

If that's the way to go then there are plenty of resources:

如果这是要走的路,那么有很多资源:

How do I run a Python script from C#?http://blogs.msdn.com/b/charlie/archive/2009/10/25/hosting-ironpython-in-a-c-4-0-program.aspx

如何从 C# 运行 Python 脚本?http://blogs.msdn.com/b/charlie/archive/2009/10/25/hosting-ironpython-in-ac-4-0-program.aspx

回答by VanDeath

I found a similar way to do it, the call of the method is much easier with it.

我找到了一种类似的方法,使用它调用方法要容易得多。

C# Code goes as follows:

C# 代码如下:

IDictionary<string, object> options = new Dictionary<string, object>();
options["Arguments"] = new [] {"C:\Program Files (x86)\IronPython 2.7\Lib", "bar"};

var ipy = Python.CreateRuntime(options);
dynamic Python_File = ipy.UseFile("test.py");

Python_File.MethodCall("test");

So basically I submit the Dictionary with the Library path which I want to define in my python file.

所以基本上我用我想在我的python文件中定义的库路径提交字典。

So the PYthon Script looks as follows:

因此 PYthon 脚本如下所示:

#!/usr/bin/python

import sys
path = sys.argv[0]  #1 argument given is a string for the path
sys.path.append(path)
import httplib
import urllib
import string

def MethodCall(OutputString):
    print Outputstring

So The method call is now much easier from C# And the argument passing stays the same. Also with this code you are able to get a custom library folder for the Python file which is very nice if you work in a network with a lot of different PC's

因此,现在从 C# 调用方法要容易得多,并且参数传递保持不变。此外,使用此代码,您可以获得 Python 文件的自定义库文件夹,如果您在具有许多不同 PC 的网络中工作,这非常好