Gen*_*ias 8 c# python ironpython
我有以下C#代码,我从C#调用python脚本:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime;
namespace RunPython
{
class Program
{
static void Main(string[] args)
{
ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);
ScriptSource source = engine.CreateScriptSourceFromFile("HelloWorld.py");
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我无法理解代码的每一行,因为我对C#的经验有限.当我运行它时,如何更改此代码以便将命令行参数传递给我的python脚本?
谢谢大家指出我正确的方向.由于某些原因,engine.sys似乎不再适用于更新版本的IronPython,因此必须使用GetSysModule.这是我的代码的修订版本,允许我更改argv:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime;
namespace RunPython
{
class Program
{
static void Main(string[] args)
{
ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);
ScriptSource source = engine.CreateScriptSourceFromFile("HelloWorld.py");
ScriptScope scope = engine.CreateScope();
List<String> argv = new List<String>();
//Do some stuff and fill argv
argv.Add("foo");
argv.Add("bar");
engine.GetSysModule().SetVariable("argv", argv);
source.Execute(scope);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7480 次 |
| 最近记录: |