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

时间:2021-12-08 20:16:01

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

我有一个应用程序由漂亮的tech-savey人使用,他们想要小岛的可编程性,所以我使用嵌入式Iron Python。

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

但是,由于IronPython 2.0 Eval()不再起作用。具体来说,我不能同时加载模块和注入局部变量。

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.

有一个工作,我仍然可以调用Execute(),打印我的答案,听取StandardOut,但然后它作为一个字符串出来,我丢失了类型。

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?

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

Cheers, Jan

干杯,Jan

2 个解决方案

#1


2  

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.

您能否对问题稍微具体一点,并提供代码示例? “eval”风格的功能肯定仍然存在,但正如您所注意到的,托管界面自1.0以来发生了很大变化。

This works in beta 5:

这适用于测试版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.

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

#2


1  

This example may help you: http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml

此示例可能对您有所帮助:http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml

#1


2  

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.

您能否对问题稍微具体一点,并提供代码示例? “eval”风格的功能肯定仍然存在,但正如您所注意到的,托管界面自1.0以来发生了很大变化。

This works in beta 5:

这适用于测试版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.

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

#2


1  

This example may help you: http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml

此示例可能对您有所帮助:http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml