脚本参数和嵌入式IronPython

cgy*_*per 4 .net c# ironpython

我的C#应用​​程序中有一个嵌入式脚本引擎,它使用IronPython 2.我创建Python运行时并向全局命名空间添加一些类,以便脚本可以将它们作为模块导入.

但是,一个(非常基本的)我无法弄清楚的是如何发送脚本参数.我意识到我可以创建一个带有参数列表的变量,但必须有一个正确的方法来完成它.此外,以正确的"Python"方式执行此操作允许我编译脚本并使用名为Sphinx的自动文档构建器.所以最终的目标是能够使用:

import sys
sys.argv
Run Code Online (Sandbox Code Playgroud)

在我的一个脚本中,让它获得用户指定的参数(通过C#app).

现在,我使用以下方法调用脚本:

// set up iron python runtime engine
_engine = Python.CreateEngine();
_runtime = _engine.Runtime;
_scope = _engine.CreateScope();

// run script
_script = _engine.CreateScriptSourceFromFile(_path);
_script.Execute(_scope);
Run Code Online (Sandbox Code Playgroud)

我已经尝试搜索一个API来添加脚本参数而没有运气.我也尝试将它们附加到脚本路径(示例中为_path),但没有运气.我尝试使用CreateScriptSourceFrom File和CreateScriptSourceFromSting(无论如何都是一个很长的镜头......).

我正在努力做甚么可能吗?

Jef*_*rdy 6

创建引擎时,可以添加包含参数的"Arguments"选项:

IDictionary<string, object> options = new Dictionary<string, object>();
options["Arguments"] = new [] { "foo", "bar" };
_engine = Python.CreateEngine(options);
Run Code Online (Sandbox Code Playgroud)

现在sys.argv将包含["foo", "bar"].

您可以设置options["Arguments"]任何实现的东西ICollection<string>.