Я хочу передать список строк из IronPython 2.6 для .NET 2.0 в программу на С# (я использую .NET 2.0, потому что я работаю с api, который запускает библиотеки DLL, созданные на основе 2.0). Но я не уверен, как отбросить его, поскольку он возвращается из ScriptEngine.
namespace test1
{
class Program
{
static void Main(string[] args)
{
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("C:\\File\\Path\\To\\my\\script.py");
ScriptScope scope = engine.CreateScope();
ObjectOperations op = engine.Operations;
source.Execute(scope); // class object created
object classObject = scope.GetVariable("MyClass"); // get the class object
object instance = op.Invoke(classObject); // create the instance
object method = op.GetMember(instance, "myMethod"); // get a method
List<string> result = (List<string>)op.Invoke(method); // call the method and get result
Console.WriteLine(result.ToString());
Console.Read();
}
}
}
мой код python имеет класс с методом, который возвращает список строк python:
class MyClass(object):
def myMethod(self):
return ['a','list','of','strings']
Я получаю эту ошибку:
Unable to cast object of type 'IronPython.Runtime.List' to type 'System.Collections.Generic.List`1[System.String]'.