我想从IronPython 2.6传递一个字符串列表给.NET 2.0的C#程序(我使用.NET 2.0是因为我要使用基于2.0构建的DLL运行的API)。但是当它从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]'.
IronPython.Runtime.List
吗? - dtb