gpt4 book ai didi

.net - 在托管(嵌入式)环境中调试 IronPython 脚本

转载 作者:行者123 更新时间:2023-12-03 09:58:33 25 4
gpt4 key购买 nike

我正在编写一个嵌入了 IronPython (2.0.1) 的 C# 应用程序。这个想法是将应用程序的一部分公开给用户编写的 IronPython 脚本。

我想让用户能够使用 Visual Studio Debugger 调试他们编写的脚本。请注意,脚本在托管环境中运行,而不是通过 IronPython 可执行文件 (ipy.exe) 运行。

在对 IronPython 程序集进行了一些 Reflector 魔法之后,我想出了一些可以让我做到这一点的东西,但我不确定这是否是规定的方式。基本上我所做的是创建一个“DebugMode”属性设置为true的“ScriptRuntime”对象,然后从“ScriptRuntime”创建一个基于python的“ScriptEngine”,我用于托管。代码如下。

        ScriptRuntimeSetup setup = new ScriptRuntimeSetup();
setup.DebugMode = true;
setup.LanguageSetups.Add(Python.CreateLanguageSetup(null));

ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);

现在,当我在托管环境中执行脚本时,使用:
            ScriptSource script = engine.CreateScriptSourceFromFile(path);
CompiledCode code = script.Compile();
ScriptScope scope = engine.CreateScope();
script.Execute(scope);

我可以在脚本文件中放置断点,当脚本执行时它们会被命中。

那么,有没有更好/更简单的方法来做到这一点?

谢谢

最佳答案

好的,我知道了。有一个“Python.CreateEngine”可以作为参数的选项字典。可以在其中指定 Debug模式。

        Dictionary<string, object> options = new Dictionary<string, object>();
options["Debug"] = true;
engine = Python.CreateEngine(options);

我认为这已经足够简单了。

关于.net - 在托管(嵌入式)环境中调试 IronPython 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/676188/

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