[IronPython] Passing arguments to C#

Mark Senko msenko at completegenomics.com
Tue Jan 4 20:02:20 CET 2011


I had tried the IList<double>, and it works fine if I send in a list that is full of doubles,
Using something like mylist = map(float,range(10))

That was an awkwardness I wanted to avoid.  I wanted to be able to pass in mylist = range(10) just as easily.
My C# routine will accept the argument as an IList (no type), but I'm unable to cast it afterwards.  
I finally solved the problem by accepting an IList, copying it element my element to a new list of the correct type. If a typecast is required, I assign it to a temporary variable of the same type first, then cast the temporary variable.

This is the function I use for my conversion of each element.  I settled on using the IronPython.Runtime.List instead of IList because that's what I'm really passing in, but they seem to act exactly the same in this context. 

        static private double pToDouble(IronPython.Runtime.List pList, int index)
        {
            string type = pList[index].GetType().FullName;

            if (type == "System.Int32")
            {
                int tmp = (int)pList[index];
                return (double)tmp;
            }
            else if (type == "System.Double")
            {
                return (double)pList[index];

            }
            else
                throw (new ApplicationException("Can't convert Python list to Double"));
        }

It's a little clumsy, but I'd rather push this down into the C# where I can hide it from my Python users.

Mark Senko

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Jeff Hardy
Sent: Tuesday, December 28, 2010 10:56 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Passing arguments to C#

On Tue, Dec 28, 2010 at 3:33 PM, Mark Senko <msenko at completegenomics.com> wrote:
> Public static void TestIt(IList inList) and public static void
> TestIt(IronPython.runtime.List inList).  They both act pretty much the same.
> I've also tried using tuples.
>

Have you tried using IList<double> or IEnumerable<double> as
arguments? IronPython should then handle the conversion for you.

- Jeff
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



____ 
 
The contents of this e-mail and any attachments are confidential and only for 
use by the intended recipient. Any unauthorized use, distribution or copying 
of this message is strictly prohibited. If you are not the intended recipient 
please inform the sender immediately by reply e-mail and delete this message 
from your system. Thank you for your co-operation.



More information about the Ironpython-users mailing list