[IronPython] Passing arguments to C#

Mark Senko msenko at completegenomics.com
Tue Dec 28 23:33:39 CET 2010

I'm trying to set up a simple scripting environment for non-programmers
that can call into our C# codebase.

Python is a non-typed language, whereas C# is strongly typed.  This is
one of the reasons IronPython was written.


It seems that the solution presented by IronPython is to expose .NET
types into Python.  If I want to call a C# function that takes an array
of doubles as an argument, I have a problem.


I know you can do something like:

Import System


To create and initialize the array with a sequence of numbers.  But now
you are working with a .NET variable, not a Python variable.

I can also do something similar with lists.


I don't want to bring variable typing into Python, because then it's not
Python.  I also don't want my users to have to worry about this extra
typing and keeping straight which variables need it and which don't.

I think the IronPython.Runtime should take care of this, but it doesn't.


I'm willing to write wrappers, in C#, for all the functions (if needed)
that I want to expose to Python. I've been knocking myself out trying to
do this.  

I've been trying to accept a non-typed list, and use it to fill an array
of doubles.  In my trial and error attempts, I've prototyped my argument

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.


If my python list is created with  pyList=range(10), then passed as an

No matter what I do, I've been unsuccessful filling my array of doubles

  myDoubles[i] = (double)inList[i];  (complains that it can't cast a
value of infinity)

or any other type of casting I can come up with.

The debugger shows a nice list, with values 0-9.

inList[i].GetType() shows System.Int32.  So why can't it cast?


As an aside, if I start with pyList=map(float,range(10)), then
GetType() returns System.double and the cast works without a problem(and
probably isn't needed).

But this requires the user to once again worry about the variable type.

Based on this, I'm pretty sure if I set up a switch statement on type in
my wrapper, and assign a temp variable with the same type, then the cast
of the temp variable it would work.

But this sure seems clumsy!


Does anyone have an idea of how to do this?  I'm sure I just haven't hit
on the right combination.

Basically I want the user to be able to use pyList = range(10) and let
me worry about converting it to double in a C# wrapper if that's what is


I'm already resigned to writing wrappers in Python to hide
clr.Reference[type]() from my users when calling a C# functions that
requires an "out" or "ref" parameters.


Thanks in advance for reading a post that I *know* is as much rant as
question.  It's just been frustrating ....




Mark Senko

Complete Genomics, Inc.

2071 Stierlin Court

Mountain View, CA 94043


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20101228/585bb7c7/attachment.html>

More information about the Ironpython-users mailing list