Think In Geek

In geek we trust

Using IronPython to extend your .NET applications

One of the interesting new things on the .NET platform is the recent addition of Python and Ruby to the CLR. Both versions for .NET are called IronPython and IronRuby respectively, and they provide some new and good things to the platform.

Python and Ruby lovers will see now that they can use all the library and features of the .NET platform programming in their favorite scripting language. Since both of them are object oriented, you can now write fully fledged apps using either of them.

However, there’s another interesting application for IronPython and IronRuby: adding scripting support for your existing .NET applications. This can be a very useful and powerful way to extend your applications and give the user freedom to program their own mini programs, scripts or whatever in your applications. It could be good for defining rules, assigning and calculating values, etc.

I’ll provide a simple class you can use to add scripting to your application. I’ll use IronPython in this example.

First of all, you have to download IronPython and install it, and add the references to the assemblies on your project references.

The usual way to proceed in those cases is to provide the user of some local variables you give them access to, execute the script, and then recover the values of those or new variables. To do so, You can use a class similar to this one:

using System;
using System.Collections.Generic;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting;
namespace Scripting
	internal class PythonEngine
        ScriptEngine m_engine;
        ExceptionOperations m_exceptionOperations;
        SortedDictionary<string, object> m_inputVariables;
        string m_script;
        internal PythonEngine()
            m_engine = Python.CreateEngine();
            m_exceptionOperations = m_engine.GetService<ExceptionOperations>();
        internal SortedDictionary<string, object> ScriptVariables
            set { m_inputVariables = value; }
        internal string Script
            set { m_script = value; }
        internal ExceptionOperations ExceptionOperations
            get { return m_exceptionOperations; }
        internal SortedDictionary<string, object> Execute()
            //Create structures
            SourceCodeKind sc = SourceCodeKind.Statements;
            ScriptSource source = m_engine.CreateScriptSourceFromString(m_script, sc);
            ScriptScope scope = m_engine.CreateScope();
            //Fill input variables
            foreach (KeyValuePair<string, object> variable in m_inputVariables)
                scope.SetVariable(variable.Key, variable.Value);
            SortedDictionary<string, object> outputVariables = new SortedDictionary<string, object>();
            //Execute the script
                //Recover variables
                foreach (string variable in scope.GetVariableNames())
                    outputVariables.Add(variable, scope.GetVariable(variable));
            catch (Exception e)
                string error = m_exceptionOperations.FormatException(e);
                //Do something with the pretty printed error
            return outputVariables;

Usage of this class is pretty simple. You have to provide the object the script you want to execute and the input variables the script will have available as local variables. Once this is done, you have to call the Execute method, and this method will either return the output variables of the execution of the resulting script, or throw an exception.

, , , , ,

One thought on “Using IronPython to extend your .NET applications

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.