Call IronPython object from C # with mono

I have the following IronPython code.

class Hello: def __init__(self): pass def add(self, x, y): return (x+y) 

I need to call this from C #, and I came up with the following code.

 using System; using IronPython.Hosting; using IronPython.Runtime; using IronPython; using Microsoft.Scripting.Hosting; using Microsoft.Scripting; class Hello { public static void Main() { ScriptEngine engine = Python.CreateEngine(); ScriptSource script = engine.CreateScriptFromSourceFile("myPythonScript.py"); ScriptScope scope = engine.CreateScope(); script.Execute(scope); } } 

After copying IronPython.dll, I run the following command. (I tried to run gacutil but got some errors .

  dmcs /r:IronPython.dll callipy.cs

But I got some error messages as follows.

  Could not load file or assembly 'Microsoft.Scripting.ExtensionAttribute, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35' or one of its dependencies.
 Missing method .ctor in assembly /Users/smcho/Desktop/cs/namespace/IronPython.dll, type System.Runtime.CompilerServices.ExtensionAttribute
 Can't find custom attr constructor image: /Users/smcho/Desktop/cs/namespace/IronPython.dll mtoken: 0x0a000080
 Could not load file or assembly 'Microsoft.Scripting.Core, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35' or one of its dependencies.
 Could not load file or assembly 'Microsoft.Scripting.Core, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35' or one of its dependencies.
 Could not load file or assembly 'Microsoft.Scripting.Core, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35' or one of its dependencies.
 ...

IronPython seems to need Microsoft.Scipting.Core, but with mono I don't know what to do?

  • Can C # run IronPython object on Mono? If so, how to do it?
+3
source share
1 answer

IronPython is not a standalone DLL. It has some dependencies that should have been sent using IronPython (they are included in the latest zip distribution with .NET 2.0 support - see the IronPython download page ):

 IronPython.Modules.dll Microsoft.Dynamic.dll Microsoft.Scripting.dll Microsoft.Scripting.Core.dll Microsoft.Scripting.Debugging.dll Microsoft.Scripting.ExtensionAttribute.dll 

Make sure your project can find these DLL files (which they currently cannot, so you get errors). I have never tried running IronPython in Mono, but it should work .

Please note that the IronPython release for .NET 4.0 does not include (or does not require) the Microsoft.Scripting.Core.dll and Microsoft.Scripting.ExtensionAttribute.dll file, as their functionality is integrated with System.Core . See this answer for more details.

+13
source

Source: https://habr.com/ru/post/1344188/


All Articles