views:

1691

answers:

2

I want to create an assembly using IronPython can call it from C#. Here are two things I am not asking.

  1. I'm not asking how to call C# from IronPython. The easiest documentation to find describes how to call C# from inside IronPython. (For example, the tutorial that ships with IronPython.) I want to do the opposite, call IronPython from C#.

  2. I'm not asking how to embed the IronPython interpreter. I've found several useful references (e.g. here and here) on how to call the IronPython interpreter from C#. That's helpful, but I'm more interested in creating a compiled assembly from IronPython. That is, I'd like to make method calls into the IronPython assembly rather than passing source code strings to the interpreter.

Once I've created the assembly, what are some tips on calling into it? One blog post I found said:

... calling a Python assembly from C# is non-trivial. That python assembly contains dynamic types, which are not easily reflected into useful C# objects.

Do you know any sort of cheat sheet for passing basic data types between IronPython and C#?

Update: The scenario I am most interested in for now is passing two or three double values into Python and getting one or two double values back. If I could pass in a string and get a string back that would be terrific, but my first priority is just passing numbers back and forth.

+3  A: 

This isn't really possible if your goal is to create an assembly which contains types that look like the types created by C#. The main problem here is that Python types work considerably differently than CLR types. For one thing, they can be mutated at runtime while CLR types are totally static. So the only way to achieve this today is to use the hosting interfaces to create a small C# stub which delegates the work to Python code.

IronPython does have the ability to compile to an assembly -- which is what that blog post refers to -- but we did this primarily so that you could deploy an IronPython application to your customers without having to give them the source code.

Feel free to follow up with a more specific scenario by adding a comment to this answer, and I'll be happy to provide advice on how to implement.

Curt Hagenlocher
Curt, thank you for your offer to help. I updated my question to provide some specifics of what I'm looking for.
John D. Cook
+1  A: 

See my answer here http://stackoverflow.com/questions/579272/instantiating-a-python-class-in-c

I am passing two integers and getting an integer back. You can do the same with doubles.

m-sharp