IronPython and C# - Script Access to C# Objects

倖福魔咒の 提交于 2019-11-29 23:26:42

问题


Consider the code below:

ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);
ScriptScope scope = engine.CreateScope();

scope.SetVariable("message", "Hello, world!");
string script = @"print message";
ScriptSource source = scope.Engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);
source.Execute();

This code yields the following exception:

Microsoft.Scripting.Runtime.UnboundNameException was unhandled Message="name 'message' is not defined"

What am I missing?


回答1:


It should be "source.Execute(scope);" instead of "source.Execute();"




回答2:


Found this: A 3 minute guide to embedding IronPython in a C# application.



来源:https://stackoverflow.com/questions/736443/ironpython-and-c-sharp-script-access-to-c-sharp-objects

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