我正在尝试从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很好?

最佳答案

此错误(带有Mono编译器的CS0009)非常笼统,在给定的程序集中表示“有问题”,因此很难调试。一种进行的方法确实是在mono下编译。如果下载其源代码(https://github.com/pythonnet),则会找到有关如何执行此操作的说明(另请参见pythonnet\src\monoclr\README.txt)。

但是,根据您的目标,您可以考虑使用IronPython代替,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
>

关于c# - Roslyn不支持for scriptcs `dynamic`,仅适用于mono,但是在DLL上mono失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32471727/

10-11 02:07
查看更多