2010-10-10 49 views
3

我有以下IronPython代碼。使用單聲道從C#調用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,但我得到了一些errors

 
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,但單我不知道該怎麼辦?

  • 可以在單聲道C#運行IronPython的對象嗎?如果是的話,那怎麼辦?

回答

13

IronPython不是獨立的DLL。它應該已使用了IronPython的一些依賴(它們包含在最新的壓縮分發針對.NET 2.0 - 看到IronPython download page):

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

確保您的項目可以找到這些DLL(它目前不能,這就是爲什麼你會得到錯誤)。我從來沒有嘗試過在單聲道上運行IronPython,但它是should work

請注意,針對.NET 4.0的IronPython版本不包含(或需要)Microsoft.Scripting.Core.dll和Microsoft.Scripting.ExtensionAttribute.dll,因爲它們的功能已合併到System.Core中。有關更多詳細信息,請參閱this answer