What the...?

Something bad happened

We're not sure what, but we have a few guesses.

Problem: Object reference not set to an instance of an object.
Ticket: 7864836
Server: Web04
Call Ironpython in C# - CodeProject Click here to Skip to main content
11,715,022 members (66,114 online)
Click here to Skip to main content

Call Ironpython in C#

, 13 Sep 2006 CPOL 33.5K 363 14
Rate this:
Please Sign up or sign in to vote.
Call ironpython in C#


Ironpython is a good script language to be used as a glue in .NET projects, especially in the parts that need to be changed frequently due to business growing or changing, or in restricted deployment environments.

In this sample, it passes the file header to python, python parses the header with regex and returns to C# function.

public string callPythonFile(string pyFile, Hashtable parameters)
    if (!File.Exists(pyFile) )
        return "Can not find "+pyFile;
    PythonEngine pe = new PythonEngine();
    if (parameters != null)
        foreach (string key in parameters.Keys)
            pe.Globals.Add(key, parameters[key]);
    MemoryStream sout = new MemoryStream();
    return ASCIIEncoding.ASCII.GetString(sout.ToArray());

To run the demo, you need .NET 2.0 or Visual Studio 2005, IronPython 1.0. You need to copy IronPython.dll to the test folder.

Python file: Test1.py

import re
if re.match('...HDR',str):
    print 'HDR'
    print 'NONE'


Hashtable ht = new Hashtable();
ht.Add("param1", param1);
outPut=new IronPythonAgent().callPythonFile(pyFile,ht);


  • 13th September, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralCalling Multiple functions from IronPaython using c# Pin
snehat21-Nov-07 0:04
membersnehat21-Nov-07 0:04 
GeneralIt doesn't support with IronPython 2.0A3 Pin
dreamwinter9-Aug-07 16:58
memberdreamwinter9-Aug-07 16:58 
GeneralVery useful article! Thanks a lot! Pin
zilibaba24-Nov-06 16:00
memberzilibaba24-Nov-06 16:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 13 Sep 2006
Article Copyright 2006 by q1andq2
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid