Script arguments and Embedded IronPython

后端 未结 2 891
情书的邮戳
情书的邮戳 2020-12-06 20:40

I have an embedded scripting engine in my C# application that uses IronPython 2. I create the Python runtime and add a few classes to the global namespace so that the script

相关标签:
2条回答
  • 2020-12-06 21:08

    When you create the engine, you can add an "Arguments" option the contains your arguments:

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

    Now sys.argv will contain ["foo", "bar"].

    You can set options["Arguments"] to anything that implements ICollection<string>.

    0 讨论(0)
  • 2020-12-06 21:08

    sys.argv is going to eventually be returning the actual parameters passed into the hosting app. Here's a few ways to solve this:

    1. Rewrite your script to accept Global Variables, then use ScriptScope.SetVariable
    2. Run the script, but rely on it to actually return you a class, which you can invoke methods on (much easier in 4.0 with dynamic keyword)
    3. Override sys.argv with your own implementation (this is a dynamic language after all!)
    0 讨论(0)
提交回复
热议问题