From IPRE Wiki
Jump to: navigation, search

This page describes how to use IronPython with your own C# code.

In the examples below, the dollar sign represents the operating system console prompt.

Setup under Linux

You may need to change your console colors to a black background.

  1. You will need the following packages:
    • mono-core mono-devel mono-winforms libgdiplus
    • These have been installed for you at Bryn Mawr College
  2. Download the Community Edition of IronPython from and unzip it
  3. cd IPCE-r7/ipy2
  4. To Run IronPython:
    • mono ipy.exe

This will run IronPython2. Type <Control+D> <ENTER> to exit.

First, test your library to see if it work in IronPython.

HelloWorld in C#

CSharp is syntactically very similar to Java. Here is a HelloWorld.cs:

using System; // Like "import" in Python; for Console

public class HelloWorld {
    public static void Main() {
        // This is a single line comment
        /* This is a
           line comment 
        Console.WriteLine("Hello World!");

Compiling C#

Mono CSharp compiler:

$ mcs HelloWorld.cs

You can then run the resulting compiled file:

$ mono HelloWorld.exe
Hello World!

Using C# inside IronPython

Use inside IronPython:

 $ mono ipy.exe
 >>> import clr
 >>> clr.AddReference("HelloWorld.exe")
 >>> import HelloWorld
 >>> HelloWorld.Main()

Writing a Library in C#

To build a DLL Library (without a Main), you compile CSharp code using the Target Library /t:library flag:

 gmcs /nologo /t:library /out:echo.dll echo.cs

That code be used in your CSharp code, or directly from Python (see below).

 gmcs /out:hello.exe /r:echo.dll hello.cs

Interacting with .NET

The Iron languages can also interact with .NET/Mono. If you are already familiar with .NET/Mono programs (such as those written in C# or Visual Basic) then you will recognize some of these idioms and names.

As a simple example, let's use the interactive abilities to directly interact with some Windows Forms. Let's create a window (called a Form in .NET parlance):

# file:
import clr
from System.Windows.Forms import *
form = Form()

If you'd like to give the window a name, enter this at the command prompt:

form.Text = "This is the Window Title"

Importing the special module named clr is the gateway to interacting with the underlying .NET/Mono subsystem. From the clr module, you can add a reference to any Dynamic Link Library (DLL) so that you can access library functions and objects. In this example, we add a reference to the System.Windows.Forms DLL which contains a library of Graphical User Interface (GUI) objects. We then can create a Form, save it in a variable named form, and start an Application:


For a series of tutorials on IronPython and WinForms, see:

Writing a C# Library

Perhaps the nicest aspect of the DLR is that you can develop code (in CSharp, for example) that is "compiled" and used directly. Such can be imported and used by any language in the DLR suite as if it were written in that language. For example, consider this simple example:

// file: Testing.cs
public class Point {
  private int _x;
  private int _y;
  public Point(int x, int y) {
	_x = x;
	_y = y;
  public int x {
	get { return _x; }
	set { _x = value; }
  public int y {
	get { return _y; }
	set { _y = value; }
  public override string ToString() {
    return "<Point at (" + _x + "," + _y +")>";

This can be compiled in Mono:

mcs Testing.cs -target:library

or put into a Project in Visual Studio, and compiled into a library.

You should then put the resulting Testing.dll somewhere will IronPython can find it (either put in the same directory with the ipy.exe, or put it in a directory and add the path of the directory to Python's sys.path):

import sys

Finally, you can use it in IronPython:

>>> import clr
>>> clr.AddReference("Testing")
>>> import Point
>>> Point
<type 'Point'>
>>> p = Point(1,2)
>>> p
<Point at (1,2)>
>>> p.x
>>> p.x = 9
>>> p
<Point at (9,2)>


Rewrite your library in CSharp, and test it in IronPython. Make some tests to see if CPython + is faster or slower than IronPython + Geometry.cs.