我正在编写一个嵌入了 IronPython (2.0.1) 的 C# 应用程序。这个想法是将应用程序的一部分暴露给用户编写的 IronPython 脚本。
我想为用户提供能够使用 Visual Studio 调试器调试他们编写的脚本的能力。请注意,脚本在托管环境中运行,而不是通过 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);
我可以在脚本文件中放置断点,当脚本执行时它们会被命中。
那么,有没有更好/更简单的方法来做到这一点?
谢谢