【问题标题】:How to call IronPython function from C#/F#?如何从 C#/F# 调用 IronPython 函数?
【发布时间】:2010-06-14 15:59:52
【问题描述】:

这是Integration of C#, F#, IronPython and IronRuby的后续问题

为了在 Python 中使用 C/C++ 函数,SWIG 是最简单的解决方案。 相反的方式也可以使用 Python C API,例如,如果我们有一个 python 函数如下

定义添加(x,y): 返回 (x + 10*y)

我们可以想出 C 中的包装器来使用这个 python,如下所示。

双加(双a,双b) { PyObject *X, *Y, *pValue, *pArgs; 双分辨率; pArgs = PyTuple_New(2); X = Py_BuildValue("d", a); Y = Py_BuildValue("d", b); PyTuple_SetItem(pArgs, 0, X); PyTuple_SetItem(pArgs, 1, Y); pValue = PyEval_CallObject(pFunc, pArgs); res = PyFloat_AsDouble(pValue); Py_DECREF(X); Py_DECREF(Y); Py_DECREF(pArgs); 返回资源; }

IronPython/C# 甚至 F# 怎么样?

  • 如何从 IronPython 调用 C#/F# 函数?或者,IronPython/C# 中是否有任何 SWIG 等效工具?
  • 如何从 C#/F# 调用 IronPython 函数?我想我可以使用“engine.CreateScriptSourceFromString”或类似的,但我需要找到一种方法来调用 IronPython 函数,看起来像一个 C#/F# 函数,而不是在字符串中编写代码,而是从文件中读取。

【问题讨论】:

标签: c# f# ironpython


【解决方案1】:

您说“现在将代码写入字符串,但从文件中读取”,那么好吧,读取文件。

来自 F# 的 Python:

let s = File.ReadAllLines("foo.py")
let engine = Python.CreateEngine()
let scriptSource = 
    engine.CreateScriptSourceFromString(s, SourceCodeKind.Statements)
...

来自 Python 的 F#:

import clr   
clr.AddReferenceToFile("SomeFsLib.dll") 

我刚从这个问题的链接中得到这些。没有尝试过,但是,它很简单,我认为它“有效”。不知道你还要求什么。

【讨论】:

    【解决方案2】:

    也许这会有所帮助: F# and Iron Python?

    【讨论】:

      【解决方案3】:

      我阅读了您上一个问题的一些答案。凯文链接的一篇文章回答了您的问题。它是在 Ruby 上的,所以也许你没有读过它。我对 DLR 了解不多,但我认为它的目的是使访问统一,因此相同的代码应该适用于 Python。

      无论如何,http://www.highoncoding.com/Articles/573_First_Look_at_the_IronRuby.aspx 在 C# 中提供了一个 .NET 4.0 示例,该示例使用 dynamic 使互操作变得超级简单。镜像您给出的 C 示例,并遵循 Brian 的代码:

      //Brian's code goes here, but C#-ified with `var` instead of `let`
      engine.Execute();
      object personClass = engine.Runtime.Globals.GetVariable("Person");
      dynamic person = engine.Operations.CreateInstance(personClass);
      person.greet();
      

      这是基于 Ruby 代码的:

      class Person
        def greet()
          puts 'hello world'
        end
      end
      

      我想可以以完全相同的方式访问等效的 Python。在我阅读与您之前的问题相关的文章之前,我不知道您可以使用 DLR 执行此操作。令人兴奋的是,C# 中的互操作如此简单。 (虽然我真的不想在 F# 中使用 dynamic,因为 F# 代码会散发出更加静态的感觉。)

      【讨论】:

        【解决方案4】:
            pyEngine = Python.CreateEngine();
                            pyScope = pyEngine.CreateScope();
                            var instance = pyEngine.Execute(@" 
                            def test(a,b): 
                            return a+b 
                            ", pyScope);
        
                            var test = pyScope.GetVariable<Func<int,int, int>>("test");
                            int s = test(2,3);
                            MessageBox.Show( Convert.ToString( test));
                            var ops = pyEngine.CreateOperations(pyScope);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多