代码之家  ›  专栏  ›  技术社区  ›  Gabriel Isenberg

Ironpython和C-脚本访问C对象

  •  4
  • Gabriel Isenberg  · 技术社区  · 16 年前

    考虑下面的代码:

    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(Microsoft.Scripting.Runtime.UnboundNameException) was unmanaged message=“名称 未定义“message”

    我错过了什么?

    2 回复  |  直到 16 年前
        1
  •  7
  •   Dino Viehland    16 年前

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

        2
  •  3
  •   Nick    16 年前