托管 IronPython 时如何设置动态导入?
我正在编写一个 C# 客户端应用程序,旨在为通过脚本进行自定义提供强大支持,并且我选择 IronPython 作为我的目标脚本语言。我想将用户的代码及其其余状态保留在应用程序的数据库中,但我需要用户能够将其脚本拆分为文件和模块。
如何配置 IronPython 引擎,使其使用字符串(带有相应的虚拟路径)作为导入源,而不是在用户文件系统中指定目录?这可能吗?
I'm writing a C# client application which is intended to have strong support for customisation via scripting, and I have chosen IronPython as my target scripting language. I would like to keep the user's code in the application's database with the rest of its state, but I need the user to be able to split their scripts into files and modules.
How can I configure the IronPython engine so that it will use strings (with corresponding virtual paths) as the source for imports, rather than specifying a directory in the user's filesystem? Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要子类化 PlatformAdaptationLayer 类并实现“文件系统”调用。然后,IronPython 将转到 PAL 对文件执行 I/O。这还涉及实现返回 PAL 类型的 ScriptHost。一个很好的例子就是 Silverlight 主机,它将文件 I/O 重定向到 XAP 文件。
如果您在 IronPython.CodePlex.com 上浏览 IronPython 源代码,您将在 IronPython_Main\Hosts\Silverlight\Microsoft.Scripting.Silverlight 中找到 Silverlight 主机。
You need to subclass the PlatformAdaptationLayer class and implement the "file system" calls. IronPython will then go to the PAL to do I/O for the files. This also involves implementing a ScriptHost which returns the type for the PAL. A good example of this is the Silverlight host which redirects file I/O to a XAP file.
If you browse the IronPython source on IronPython.CodePlex.com you'll find the Silverlight host in IronPython_Main\Hosts\Silverlight\Microsoft.Scripting.Silverlight.