2012-07-11 65 views
0

我跟隨this。 從C#調用IronPython的有MonoDev的一個簡單的例子:如何使Python.CreateEngine()工作? - TargetInvocationException

的Python:

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

我有幾個問題與組件做同類的例子。現在我的問題是,每個程序試圖時間做: ScriptEngine engine = Python.CreateEngine();, 我得到以下錯誤:

System.Reflection.TargetInvocationException: 
Failed to load language 'IronPython 2.7.3': 
An exception was thrown by the type initializer for 
IronPython.Runtime.ExtensionMethodSet ---> System.Exception: 
An exception was thrown by the type initializer for 
IronPython.Runtime.ExtensionMethodSet ---> System.Exception: 
Could not load type 'IronPython.Runtime.ExtensionMethodSet+AssemblyLoadInfo[]' 
from assembly 'IronPython, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1'. 

以下建議在this forum發現, 我得承認,我沒有Microsoft.Scripting.Debugging.dll,因爲我不知道它可以下載到哪裏 - 它沒有提供IronPython。 你能告訴我在哪裏可以得到它,如果這是我仍然堅持這個基本例子的原因?

+0

請注意版本號中的不匹配。這是你會從中得到的那種錯誤。 – 2012-07-11 01:58:20

+0

非常感謝。現在我使用IronPython 2.7.0而不是最新版本,而且一切都很完美。 – ssx 2012-07-11 10:01:01

回答

0
ScriptScope scope = engine.CreateScope(); 

dynamic scope = engine.CreateScope(); 

ScriptScope基類

相關問題