<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Think In Geek &#187; ironruby</title>
	<atom:link href="http://thinkingeek.com/tag/ironruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkingeek.com</link>
	<description>In geek we trust</description>
	<lastBuildDate>Sat, 19 Jun 2010 22:00:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using IronPython to extend your .NET applications</title>
		<link>http://thinkingeek.com/2009/03/11/using-ironpython-to-extend-your-net-applications/</link>
		<comments>http://thinkingeek.com/2009/03/11/using-ironpython-to-extend-your-net-applications/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 15:44:32 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[ironpython]]></category>
		<category><![CDATA[ironruby]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=93</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>
One of the interesting new things on the .NET platform is the recent addition of <a href="http://www.python.org/" onclick="pageTracker._trackPageview('/outgoing/www.python.org/?referer=');">Python</a> and <a href="http://ruby-lang.org/" onclick="pageTracker._trackPageview('/outgoing/ruby-lang.org/?referer=');">Ruby</a> to the CLR. Both versions for .NET are called <a href="http://www.codeplex.com/IronPython" onclick="pageTracker._trackPageview('/outgoing/www.codeplex.com/IronPython?referer=');">IronPython</a> and <a href="http://www.ironruby.net/" onclick="pageTracker._trackPageview('/outgoing/www.ironruby.net/?referer=');">IronRuby</a> respectively, and they provide some new and good things to the platform.
</p>
<p>
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.
</p>
<p>
However, there&#8217;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.
</p>
<p>
I&#8217;ll provide a simple class you can use to add scripting to your application. I&#8217;ll use IronPython in this example.
</p>
<p>
First of all, you have to download IronPython and install it, and add the references to the assemblies on your project references.
</p>
<p>
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:
</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">IronPython.Hosting</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.Scripting.Hosting</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.Scripting</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> Scripting
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">class</span> PythonEngine
	<span style="color: #000000;">&#123;</span>
        ScriptEngine m_engine<span style="color: #008000;">;</span>
        ExceptionOperations m_exceptionOperations<span style="color: #008000;">;</span>
        SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> m_inputVariables<span style="color: #008000;">;</span>
        <span style="color: #FF0000;">string</span> m_script<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> PythonEngine<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            m_engine <span style="color: #008000;">=</span> Python.<span style="color: #0000FF;">CreateEngine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            m_exceptionOperations <span style="color: #008000;">=</span> m_engine.<span style="color: #0000FF;">GetService</span><span style="color: #008000;">&lt;</span>ExceptionOperations<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> ScriptVariables
        <span style="color: #000000;">&#123;</span>
            set <span style="color: #000000;">&#123;</span> m_inputVariables <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">string</span> Script
        <span style="color: #000000;">&#123;</span>
            set <span style="color: #000000;">&#123;</span> m_script <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> ExceptionOperations ExceptionOperations
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> m_exceptionOperations<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> Execute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//Create structures</span>
            SourceCodeKind sc <span style="color: #008000;">=</span> SourceCodeKind.<span style="color: #0000FF;">Statements</span><span style="color: #008000;">;</span>
            ScriptSource source <span style="color: #008000;">=</span> m_engine.<span style="color: #0000FF;">CreateScriptSourceFromString</span><span style="color: #000000;">&#40;</span>m_script, sc<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ScriptScope scope <span style="color: #008000;">=</span> m_engine.<span style="color: #0000FF;">CreateScope</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//Fill input variables</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>KeyValuePair<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> variable <span style="color: #0600FF;">in</span> m_inputVariables<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                scope.<span style="color: #0000FF;">SetVariable</span><span style="color: #000000;">&#40;</span>variable.<span style="color: #0000FF;">Key</span>, variable.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> outputVariables <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//Execute the script</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                source.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span>scope<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">//Recover variables</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> variable <span style="color: #0600FF;">in</span> scope.<span style="color: #0000FF;">GetVariableNames</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    outputVariables.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>variable, scope.<span style="color: #0000FF;">GetVariable</span><span style="color: #000000;">&#40;</span>variable<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> error <span style="color: #008000;">=</span> m_exceptionOperations.<span style="color: #0000FF;">FormatException</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">//Do something with the pretty printed error</span>
                throw<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> outputVariables<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</p>
<p>
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.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fthinkingeek.com%2F2009%2F03%2F11%2Fusing-ironpython-to-extend-your-net-applications%2F&amp;linkname=Using%20IronPython%20to%20extend%20your%20.NET%20applications" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fthinkingeek.com_2F2009_2F03_2F11_2Fusing-ironpython-to-extend-your-net-applications_2F_amp_linkname=Using_20IronPython_20to_20extend_20your_20.NET_20applications&amp;referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2009/03/11/using-ironpython-to-extend-your-net-applications/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
