Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a .NET assembly in IronPython and call it from C#?

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.

like image 389
John D. Cook Avatar asked Feb 18 '09 15:02

John D. Cook


People also ask

How can I produce an assembly in C#?

It's pretty easy to create an assembly from scratch. In Microsoft Visual C# 2008 Express Edition, choose File > New Project, select Class Library as the project type, enter a name (if you want a non-default name), and click OK. The wizard will chug away and then create your brand-new class library.

What is .NET assembly in C#?

NET-based applications. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (. dll) files, and are the building blocks of . NET applications.

What is a .NET assembly file?

NET defines a binary file format, assembly, that is used to fully describe and contain . NET programs. Assemblies are used for the programs themselves as well as any dependent libraries. A . NET program can be executed as one or more assemblies, with no other required artifacts, beyond the appropriate .

Is .NET assembly language?

C# Assembly is a standard library developed for . NET. Common Language Runtime, CLR, MSIL, Microsoft Intermediate Language, Just In Time Compilers, JIT, Framework Class Library, FCL, Common Language Specification, CLS, Common Type System, CTS, Garbage Collector, GC.


1 Answers

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.

like image 137
Curt Hagenlocher Avatar answered Oct 06 '22 01:10

Curt Hagenlocher