我在我的 C# 应用程序中嵌入了 IronPython。
出于某种原因,我在加载程序集时遇到了问题。具体来说,我想要 System.dll,这样我就可以访问 DateTime 等 .NET 类。

如果我尝试这一行:

_runtime.LoadAssembly(_runtime.Host.PlatformAdaptationLayer.LoadAssembly("System"));

我得到:
could not load file or assembly 'System'

如果我明确输入 C:/WINDOWS/Microsoft.NET/.../System.dll 的路径,我会得到:
The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

然后我尝试在 Python 脚本中使用 clr 进行导入:
import clr
clr.AddReference('System')
from System import DateTime

现在我得到:
Cannot import name DateTime

我哪里错了?为什么DateTime 不在System 中,为什么LoadAssembly 找不到System.dll?我是否需要明确地为 IronPython 设置一些搜索路径?是否找到无效的“系统”?

当我在 IronPython 解释器中测试时,这一切正常。

最佳答案

我使用 engine.Runtime.LoadAssembly(typeof(string).Assembly); 来加载系统程序集;我相信这也是 IronPython 控制台的工作方式。

附言不要忘记 source to IronPython is available ;这是像这样的东西的金矿。

关于.net - 嵌入 IronPython - 无法添加对系统的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1394679/

10-11 23:21
查看更多