Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C#/F#调用IronPython函数?_C#_F#_Ironpython - Fatal编程技术网

如何从C#/F#调用IronPython函数?

如何从C#/F#调用IronPython函数?,c#,f#,ironpython,C#,F#,Ironpython,这是一种后续问题 为了使用Python中的C/C++函数,SWIG是最简单的解决方案。 例如,如果我们有一个如下所示的Python函数,那么使用pythoncapi也可以使用相反的方法 def add(x,y): return (x + 10*y) def添加(x,y): 返回(x+10*y) 我们可以用C编写包装器来使用这个python,如下所示 double Add(double a, double b) { PyObject *X, *Y, *pValue, *pArgs;

这是一种后续问题

为了使用Python中的C/C++函数,SWIG是最简单的解决方案。 例如,如果我们有一个如下所示的Python函数,那么使用pythoncapi也可以使用相反的方法

def add(x,y): return (x + 10*y) def添加(x,y): 返回(x+10*y) 我们可以用C编写包装器来使用这个python,如下所示

double Add(double a, double b) { PyObject *X, *Y, *pValue, *pArgs; double res; 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); return res; } 双加(双a,双b) { PyObject*X,*Y,*pValue,*pArgs; 双res; pArgs=PyTuple_New(2); X=Py_构建值(“d”,a); Y=Py_构建值(“d”,b); PyTuple_集项(pArgs,0,X); PyTuple_集合项(pArgs,1,Y); pValue=PyEval_CallObject(pFunc,pArgs); res=PyFloat_AsDouble(pValue); Py_DECREF(X); Py_DECREF(Y); Py_DECREF(pArgs); 返回res; } IronPython/C甚至F如何

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

    • 您说“现在以字符串形式编写代码,但从文件中读取”,所以,好的,读取文件

      来自F#的Python:

      F#来自Python:

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

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

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

      无论如何,给出了一个C语言中的.NET4.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#中的
      动态
      ,因为F#代码给人一种更静态的感觉。)

      也许这可以帮助: ?

      pyEngine=Python.CreateEngine();
      pyScope=pyEngine.CreateScope();
      var instance=pyEngine.Execute(@)
      def测试(a、b):
      返回a+b
      “,pyScope);
      var test=pyScope.GetVariable(“test”);
      int s=试验(2,3);
      Show(Convert.ToString(test));
      var ops=pyEngine.CreateOperations(pyScope);
      
      问题是什么?您链接的问题没有回答的问题是什么?另请参见例如。
      class Person
        def greet()
          puts 'hello world'
        end
      end
      
          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);