die Weitergabe von Listen von IronPython zu C#

Möchte ich weitergeben, eine Liste von strings von IronPython 2.6 .NET 2.0, C# - Programm (ich bin mit .NET 2.0, weil ich arbeite mit einer api, läuft der DLLs gebaut auf 2.0). Aber ich bin mir nicht sicher, wie Sie Sie zu werfen, wie es kommt zurück von der 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();
        }
    }
}

mein python-code eine Klasse mit einer Methode, gibt eine python-Liste von strings:

class MyClass(object):
    def myMethod(self):
        return ['a','list','of','strings']

Bekomme ich diesen Fehler:

Unable to cast object of type 'IronPython.Runtime.List' to type 'System.Collections.Generic.List`1[System.String]'.

InformationsquelleAutor der Frage BenjaminGolder | 2011-03-06

Schreibe einen Kommentar