What's the best way to embed IronPython inside my C# App?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 01:25:32

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/199390/whats-the-best-way-to-embed-ironpython-inside-my-c-sharp-app

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