2011-03-08 84 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