簡體   English   中英

使用mono調用C#中的IronPython對象

[英]Calling IronPython object from C# with mono

我有以下IronPython代碼。

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

我需要從C#調用它,我想出了以下代碼。

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);
    }
}

復制IronPython.dll后,我運行以下命令。 (我試圖運行gacutil,但是我遇到了一些錯誤

dmcs /r:IronPython.dll callipy.cs

但是我得到了一些錯誤信息,如下所示。

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需要Microsoft.Scipting.Core,但是使用mono我不知道該怎么辦?

  • C#可以在Mono上運行IronPython對象嗎? 如果是這樣,怎么辦?

IronPython不是一個獨立的DLL。 它有一些應該與IronPython一起提供的依賴項(它們包含在針對.NET 2.0的最新壓縮分發中 - 請參閱IronPython下載頁面 ):

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

確保您的項目可以找到這些DLL(它目前無法找到,這就是您收到錯誤的原因)。 我從來沒有嘗試過在Mono上運行IronPython,但它應該可行

請注意,針對.NET 4.0的IronPython版本不包含(或需要)Microsoft.Scripting.Core.dll和Microsoft.Scripting.ExtensionAttribute.dll,因為它們的功能已合並到System.Core 有關詳細信息,請參閱此答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM