2011-03-08 87 views
1

我想用c#運行IronPython代碼。 我創建了一個簡單的控制檯應用程序(.NET 4),並加入IronPython.dll,IronPython.Modules.dll和Microsoft.Scripting並寫下了下面的代碼 -IronPython微軟。腳本異常

using Microsoft.Scripting.Hosting; 

namespace app 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      ScriptRuntime runtime = IronPython.Hosting.Python.CreateRuntime(); 
      ScriptEngine engine = runtime.GetEngine("py"); 

      engine.Execute("print 'hello!'"); 
     } 
    } 
} 

試圖運行這個,我得到一個異常 -

未處理的異常: System.Reflection.TargetInvocationException: 異常已被調用的 目標拋出。 ---> System.IO.FileNotFoundException:找不到 無法加載文件或組件 'Microsoft.Scripting,版本= 1.0.0.0, 文化 =中性公鑰= 31bf3856ad364e35' 或一個依賴的 之一。系統m 找不到指定的文件。在 Core.LanguageProvider.Python..ctor(流 流)---內部異常 堆棧跟蹤---結束在 System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo 方法,O- bject []指定參數時, SignatureStruct &簽名, RuntimeType declaringType)在 System.RuntimeMethodHandle.InvokeConstructor(IRuntimeMethodInfo 方法,OB JECT []指定參數時, SignatureStruct簽名,RuntimeType declaringType)在 System.Reflection.RuntimeConstructorInfo.Invoke(的BindingFlags invokeAttr,B因德爾粘合劑,對象[] 參數,CultureInfo文化)在 System.RuntimeType.CreateInstanceImpl(的BindingFlags bindingAttr,捆紮機倉DER,對象[] 指定參數時,CultureInfo的培養,在 System.Activator.CreateInstance(類型 型,的BindingFlags bindingAttr對象[] activationAttributes),賓德 ř粘合劑,對象[]指定參數時,CultureInfo的 培養,在 System.Activator.CreateInstance(類型 型對象[] activationAttributes),在 Core.LanguageRuntime.RegisterLanguageProvider(類型 providerType)在d對象[]參數):\代碼 e \ CodeRunner \ Core \ LanguageRuntime.cs:行 30 at Test.Program.Main(String [] 參數 D:\ code \ CodeRunner \ Test \ Program.cs:lin e 19按任意鍵繼續。 。 。

我真的,不要t know what to do , searched at google and don找到解決辦法。 我很樂意獲得幫助。

回答

2

您使用的是什麼版本的鐵蟒?告訴你的語法似乎屬於舊版本,現在你應該有這樣的事情:

var ipy = Python.CreateRuntime(); 

反正this blog post似乎是指向正確的參考使用真正準確。無論如何,確保你有latest IronPython version.

+0

謝謝,解決了它。我試圖運行ironruby代碼和ironpython時遇到了另一個問題,我收到了這個錯誤,該怎麼辦? – Yosi 2011-03-08 20:26:18

+0

我不明白你的回覆是否適合你。無論如何,如果您使用兩個版本(取決於相同的DLR),您應該能夠運行IronPython和IronRuby。不幸的是,我只有IronPython的經驗,所以我不能再多說Ruby了。 – 2011-03-08 20:56:29

+0

它爲我工作。我無法運行,因爲IronPython使用Microsoft.Scripting版本2.6和使用Microsoft.Scripting版本1.1的IronRuby,該怎麼辦? – Yosi 2011-03-08 21:24:41