我正在构建一个IronPython程序,该程序将从URL执行指定的* .dll。这是一个片段:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Net;
using IronPython.Runtime.Operations;
public class Program
{
[STAThread]
public static void my()
{
WebClient wc = new WebClient();
PythonOps.InitializeModuleEx(Assembly.Load(wc.DownloadData("https://data/from/url.dll")), "__main__", null, false, null);
}
public static void Main(string[] args)
{
Program.my();
}
}
它使用功能:PythonOps.InitializeModuleEx
来执行此操作。一切正常,只是问题在于它接受了Environment.GetCommandLineArgs
的参数。我查看了该函数的pythonops.cs部分,并注意到,如果options == null
会得到Environment.GetCommandLineArgs
,这是我不想要的。以下是来自PythonOps.cs的参考:
public static int InitializeModuleEx(Assembly/*!*/ precompiled, string/*!*/ main, string[] references, bool ignoreEnvVars, Dictionary<string, object> options) {
ContractUtils.RequiresNotNull(precompiled, nameof(precompiled));
ContractUtils.RequiresNotNull(main, nameof(main));
if(options == null) {
options = new Dictionary<string, object>();
}
options["Arguments"] = Environment.GetCommandLineArgs();
var pythonEngine = Python.CreateEngine(options);
var pythonContext = (PythonContext)HostingHelpers.GetLanguageContext(pythonEngine);
我需要帮助来了解解决此问题的最佳方法。我应该只创建一个带有一个参数的示例选项,以便不调用此函数吗?如果是这样,我该怎么办?
总而言之,我只是想在代码中使用硬编码的参数将其传递给程序,而不是从命令行读取它们。
更新2:
感谢您的回答,这是我修改的代码:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Net;
using IronPython.Runtime.Operations;
public class Program
{
public static int InitializeModuleExDefault(
Assembly precompiled,
string main,
string[] references,
bool ignoreEnvVars)
{
IDictionary<string, object> options = new Dictionary<string, object>();
options.Add("Arguments", new Object {}); // replace with whatever object you want
}
public static int Main()
{
int InitializeModuleExDefault;
try
{
WebClient wc = new WebClient();
InitializeModuleExDefault = PythonOps.InitializeModuleEx(Assembly.Load(wc.DownloadData("https://data/from/url.dll")), "__main__", null, false);
}
catch (Exception ex)
{
string text = ex.Message;
Console.WriteLine("Error occurred: {0}", text);
InitializeModuleExDefault = -1;
}
return InitializeModuleExDefault;
}
}
但是它不会编译,因为并不是所有的代码路径都返回一个值。