【问题标题】:IronPython and C# - Script Access to C# ObjectsIronPython 和 C# - 对 C# 对象的脚本访问
【发布时间】:2010-10-18 16:44:27
【问题描述】:

考虑下面的代码:

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 “消息”未定义”

我错过了什么?

【问题讨论】:

    标签: c# ironpython


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多