简体   繁体   English

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

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

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: Python代码:

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脚本中的一个方法?

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

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文件中定义的Library路径提交Dictionary。

So the PYthon Script looks as follows: 所以PYthon Script看起来如下:

#!/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. 因此,从C#开始,方法调用变得更加容易,并且参数传递保持不变。 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 使用此代码,您还可以获得Python文件的自定义库文件夹,如果您在具有许多不同PC的网络中工作,这非常好。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM