对于scriptcs,“动态”不受Roslyn支持,只有单声道可用,但单声道在DLL上失败

时间:2015-09-09 05:44:40

标签: c# mono roslyn python.net scriptcs

我尝试从scriptcs加载pythonnet运行时dll并且它不能与roslyn后端一起使用,因为Roslyn不支持动态,但单声道后端阻塞会出现以下错误:

$ scriptcs -modules mono
scriptcs (ctrl-c to exit or :help for help)

> #r "F:\Python\Python27\Lib\site-packages\Python.Runtime.dll"
error CS0009: Metadata file `F:\Python\Python27\Lib\site-packages\Python.Runtime.dll' does not contain valid metadata

问题:

如何使用Python.Runtime.DLL获取编写脚本的Mono后端?我需要在此之前加载任何DLL吗?是否必须使用Mono支持编译Python.Runtime.DLL或.NET是否可以?

1 个答案:

答案 0 :(得分:4)

此错误(CS0009与单声道编译器)非常通用,对于给定的程序集意味着“出错”,因此很难调试。一种方法是在mono下编译。如果您下载他们的源代码(https://github.com/pythonnet),您将找到有关如何执行此操作的说明(另请参阅pythonnet \ src \ monoclr \ README.txt)。

但是,根据您的目标,您可以考虑使用IronPython,这是由mono正式支持并且开箱即用。下面的示例假设您下载了IronPython的zip并将其解压缩到某个地方:

scriptcs -modules mono
scriptcs (ctrl-c to exit or :help for help)
> #r "G:\dev_tools\IronPython-2.7.5\IronPython-2.7.5\IronPython.dll"
> #r "G:\dev_tools\IronPython-2.7.5\IronPython-2.7.5\Microsoft.Dynamic.dll"
> #r "G:\dev_tools\IronPython-2.7.5\IronPython-2.7.5\Microsoft.Scripting.dll"
> using System;
> using System.IO;
> using IronPython.Hosting;
> using Microsoft.Scripting;
> using Microsoft.Scripting;
> using Microsoft.Scripting.Hosting;
> string script = "print 'hello world'";
> var engine = Python.CreateEngine();
> var scope = engine.CreateScope();
> var source = engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);
> var compiled = source.Compile();
> var result = compiled.Execute(scope);
hello world
>