将 IronPython 嵌入到我的 C# 应用程序中的最佳方法是什么?

发布于 2024-07-07 23:13:14 字数 326 浏览 4 评论 0原文

我有一个由相当精通技术的人使用的应用程序,他们想要可编程性的小岛,所以我使用了嵌入式 Iron Python。

然而,自 IronPython 2.0 Eval() 起不再起作用。 具体来说,我无法同时加载模块和注入局部变量。

有一个解决办法,我仍然可以调用 Execute(),打印出我的答案并收听 StandardOut,但随后它会以字符串形式出现,并且我丢失了类型。

这并不是一场长期的灾难,但对于更复杂的对象来说却是一个巨大的痛苦。

有谁知道如何让 Eval() 在 2.0 中像在 1.x 中那样再次工作?

干杯, 扬

I have an application used by pretty tech-savey people and they want small island of programmability so I've used embedded Iron Python.

However, since IronPython 2.0 Eval() doesn't work any more. Specifically I can't both load modules and inject local variables.

There is a work around where I can still call Execute(), print out my answer and listen to StandardOut, but then it comes out as a string and I've lost the type.

Not a disaster for a long, but a huge pain for more complex objects.

Does anyone know how to get Eval() working again in 2.0 like it did in 1.x?

Cheers,
Jan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-07-14 23:13:14

您能否更具体地说明问题,并提供一个代码示例? “eval”风格的功能肯定仍然存在,尽管正如您所注意到的,自 1.0 以来,托管接口已经发生了很大的变化。

这在 beta 5 中有效:

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString("2 + 5", SourceCodeKind.Expression);
int result = source.Execute<int>();

顺便说一句,使用正确的 SourceCodeKind 在这里至关重要。

Could you be a little more specific about the problem, and maybe provide a code example? The "eval"-style functionality is definitely still present, though as you've noticed, the hosting interface has changed considerably since 1.0.

This works in beta 5:

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString("2 + 5", SourceCodeKind.Expression);
int result = source.Execute<int>();

By the way, using the right SourceCodeKind is critical here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文