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

考虑以下代码:

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

此代码产生以下exception:

Microsoft.Scripting.Runtime.UnboundNameException未处理Message =“name’message’未定义”

我错过了什么?

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

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