简体   繁体   中英

Passing lists from IronPython to C# (2)

There's already a similar answered question in this post but this one is different: I actually do not get a IronPython.Runtime.List (I do have using System.Linq ;) from a call like Func<IList<double>> get_a_list = pyOps.GetMember<Func<IList<double>>>(class1, "get_a_list"); where get_a_list is the Python code that returns a Python [] list. I get a {IronPython.Runtime.ListGenericWrapper<double>} . How can I convert that to a C# List?

See the code below - all works fine except for getting the x variable into C#.

class class1(object):
"""Support demo of how to call python from C#"""
def __init__(self):
    self.a = 5
def get_a_square(self):
    return pow(self.a,2)
def get_a_times_b(self, b):
    return self.a*b
def get_a_list(self):
    return [self.a,self.a,self.a]

and the C# Code is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Reflection;
using System.IO;

namespace DemoUsingPythonModule
{
    class Program
    {
        static void Main(string[] args)
        {
            ScriptEngine pyEngine = Python.CreateEngine();

            // Load the DLL and the Python module
            Assembly dpma = Asse   mbly.LoadFile(Path.GetFullPath("DemoPythonModule.dll"));
            pyEngine.Runtime.LoadAssembly(dpma);
            ScriptScope pyScope = pyEngine.Runtime.ImportModule("class1");
            ObjectOperations pyOps = pyEngine.Operations;

            // Instantiate the Python Class
            var classObj = pyScope.GetVariable("class1");
            object class1 = pyOps.Invoke(classObj);

            // Invoke a method of the class
            var a2 = pyOps.InvokeMember(class1, "get_a_square", new object[0]);
            Console.Write(a2.ToString()+'\n');

            // create a callable function to 'get_a_square'
            Func<double> get_a_square = pyOps.GetMember<Func<double>>(class1, "get_a_square");
            double a2_2 = get_a_square();

            // create a callable function to 'get_a_times_b'
            Func<double, double> get_a_times_b = pyOps.GetMem    ber<Func<double, double>>(class1, "get_a_times_b");                       
            Console.WriteLine(get_a_times_b(3.0).ToString());
            Console.WriteLine(get_a_times_b(4.0).ToString());

            Func<IList<double>> get_a_list =       pyOps.GetMember<Func<IList<double>>>(class1, "get_a_list");
            var x = get_a_list();
            Console.WriteLine(x.Cast<double>().ToList().ToString());

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

OK I found it. The type is indeed IronPython.Runtime.ListGenericWrapper<double> but the Python code above returns a list of ints. If I change the constructor to self.a = 5.0 the thing works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM