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

これを見つけました: A 3 minutes guide to embedded IronPython in a C# application

于 2009-04-17T10:38:46.323 に答える