4

考虑下面的代码:

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="name 'message' is not defined"

我错过了什么?

4

2 回答 2

7

它应该是“source.Execute(scope);” 而不是“source.Execute();”

于 2009-04-10T01:59:20.937 回答
3

找到这个:在 C# 应用程序中嵌入 IronPython 的 3 分钟指南

于 2009-04-17T10:38:46.323 回答