Embedding IronPython - Can't add reference to System

拈花ヽ惹草 提交于 2019-12-01 06:36:26

问题


I'm embedding IronPython in my C# application. For some reason I'm having trouble loading assemblies. Specifically, I want System.dll so I can have access to .NET classes like DateTime.

If I try the line:

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

I get:

could not load file or assembly 'System'

If I explicitly type the path to C:/WINDOWS/Microsoft.NET/.../System.dll I get:

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

So then I tried doing the import using clr inside the Python script:

import clr
clr.AddReference('System')
from System import DateTime

And now I get:

Cannot import name DateTime

Where am I going wrong? Why is DateTime not in System, and why can't LoadAssembly find System.dll? Do I need to explicity set some search paths for IronPython? Is it finding an invalid 'System'?

This all works fine when I test in the IronPython interpreter.


回答1:


I use engine.Runtime.LoadAssembly(typeof(string).Assembly); to get the System assembly loaded; I believe this is how the IronPython console does it as well.

P.S. Don't forget that the source to IronPython is available; it's a gold mine for stuff like this.



来源:https://stackoverflow.com/questions/1394679/embedding-ironpython-cant-add-reference-to-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!