2009-04-10 39 views
4

考慮下面的代碼: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(); 

此代碼產生以下例外:

Microsoft.Scripting.Runtime.UnboundNameException 了未處理消息=」名稱 '消息'未定義「

我錯過了什麼?

回答

7

它應該是「source.Execute(scope);」而不是「source.Execute();」