IronPython和C# - 脚本访问C#对象

4

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();

这段代码会产生以下异常:

Microsoft.Scripting.Runtime.UnboundNameException未处理。消息为“未定义名称'message'”。

我缺少什么?

2个回答

7

应该使用"source.Execute(scope);"而不是"source.Execute();"。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接