SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

Converting C# and VB.NET Code to IronPython

SharpDevelop 3.1 now supports converting C# and VB.NET code to IronPython. It can convert a single file or an entire project. The code to convert between these languages is still under development and has some limitations.

Converting an Individual File

To convert a C# or VB.NET file, open it in SharpDevelop's text editor, then from Tools menu select Convert code to Python.

Convert code to Python menu option.

The code conversion is limited to converting classes so it will not convert an arbitary piece of code that is not inside a class.

C# code before conversion.

C# code after conversion to Python

Converting a Project

To convert a C# or VB.NET project, open it in SharpDevelop, then from the Project menu select Convert From C# to Python.

Convert from C# project to Python project menu option.

Once converted the project will most likely not compile straight away due to limitations in the implementation. At the time of writing converting a project has the following limitations:

  • Project's Main File is not set.
  • No code generated to call the project's Main entry method.
  • Namespace imports do include all the used classes.

Code Conversion Internals

Converting code to IronPython was originally supported in SharpDevelop 2.2 and was based on converting code to a Microsoft CodeDOM and then getting IronPython 1.0 to generate the Python code. In IronPython 2.0 this CodeDOM support was removed so the code conversion feature was removed from SharpDevelop 3.0 since that was using IronPython 2.0. In SharpDevelop 3.1 the code conversion has been rewritten to no longer use the CodeDOM support. It now works by executing the following simple steps:

  1. The C# or VB.NET code is parsed using SharpDevelop's parsing library NRefactory and an abstract syntax tree (AST) is generated.
  2. A visitor class then walks this AST and generates Python code which is added to a StringBuilder.
  3. Once the visit is complete the generated Python code is then displayed or saved to disk.
Published May 11 2009, 08:07 PM by MattWard
Filed under: ,



Nivaldo Fernandes said:

Hi Matt,

First and foremost let me say to you how impressed I am with the converter feature. Great job!

I was skeptical at first but decided to put it to a tough test (in my opinion): convert a C# program with 6,779 lines of code to IronPython (IP). The results were so encouraging...after spending some 3 hours or so adjusting the convertion, I had an IronPython version of the program with 3,955 working just right.

It would have taken an hour or so less if it were not for the fact that I had a XML settings file that I serialized and de-serialized using serialization attributes...and declarative attributes do not yet work on IronPython. I had to substitute that for the  settings file techinque in the IronPython cookbook site (with minor adaptations).

But back to the converter and in the spirit of giving you some feedback for the continuing improvement of it (please continue with this great project!), here are some issues that I encountered and that required adjustments:

1) Dictionary and List declarations did not work too well.

Here is the original declaration in C#:

Dictionary<string, string> record = new Dictionary<string,string>();

List<string> methods = new List<string>();

And here are the resulting IP declarations:

record = Dictionary()

methods = List()

I fixed this by making the following adjustments in the IP declarations:

record = Dictionary[str,str]()

methods = List[str]()

So, no biggie, but something to keep in mind...

2) A switch statement with  break did not convert correctly to IP.

Here is the a piece of the switch statement in C#:

switch (dtype)


   case "1": record.Add("FirstPostingDay", day);


   case "2": record.Add("AcceptedDay", day);



Here is the resulting IP if-else stements:

if dtype == "1":

self._record.Add("FirstPostingDay", day)


elif dtype == "2":

self._record.Add("AcceptedDay", day)



The fix was easy, just to remove the break statements in IP, but I only found it when I ran the program against certain, one thing to watch out for in the Converter although it might prove difficult to programmatically decide whether the break statement is "legitimate".

3) While loop with assignment did not convert right?

Here is the while loop in C#:

while ((uid = file.ReadLine()) != null)





Here is the IP equivalent:

while (uid = file.ReadLine()) != None:

ct += 1


For me, the IP interpreter did not like the assignment inside the While loop...and I think Python does not allow it...but I may be wrong...I am not a Python expert...I am more of a .NET developer scripting .NET in IronPython.

So, this is basically what I can remember that gave me trouble. I hope this helps out. Again, I am nothing but impressed with the job you guys did. Keep it up!


Nivaldo Fernandes

May 31, 2009 5:45 PM

MattWard said:

Thanks for the bug reports.

1) and 2) are fixed in

3) is not fixed. Not sure what to do about this. A possible equivalent while loop would be:

uid = file.ReadLine();

while (uid != null)




   uid = file.ReadLine();


June 3, 2009 8:55 PM
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this ( email address.