gpt4 book ai didi

c# - IronPython 和 C# - 对 C# 对象的脚本访问

转载 作者:太空狗 更新时间:2023-10-30 00:22:34 24 4
gpt4 key购买 nike

考虑下面的代码:

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 was unhandled Message="name 'message' is not defined"

我错过了什么?

最佳答案

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

关于c# - IronPython 和 C# - 对 C# 对象的脚本访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/736443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com