<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Ryan Dawson's Web Log</title><link>http://blogs.msdn.com/b/rdawson/</link><description /><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Hosting IronPython 2.0 Alpha 6 via the DLR</title><link>http://blogs.msdn.com/b/rdawson/archive/2007/11/29/hosting-ironpython-2-0-alpha-6-via-the-dlr.aspx</link><pubDate>Fri, 30 Nov 2007 00:59:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6603455</guid><dc:creator>rdawson</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/rdawson/rsscomments.aspx?WeblogPostID=6603455</wfw:commentRss><comments>http://blogs.msdn.com/b/rdawson/archive/2007/11/29/hosting-ironpython-2-0-alpha-6-via-the-dlr.aspx#comments</comments><description>&lt;p&gt;We previously discussed the &lt;a href="http://blogs.msdn.com/rdawson/archive/2007/11/28/hosting-ironpython-1-1.aspx"&gt;hosting model of IronPython 1.1&lt;/a&gt;.&amp;#160; This time around we'll see how that model was updated in the &lt;a href="http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=7663"&gt;latest Alpha release&lt;/a&gt; by reviewing an updated version of the same example.&lt;/p&gt;  &lt;p&gt;It should be noted that the model presented here is changing very soon.&amp;#160; So why discuss it?&amp;#160; Well, two reasons.&amp;#160; The first is practical in that this is the model you'll deal with when using the latest binaries.&amp;#160; But also, I think some of you might find the insight into our model's evolution interesting.&lt;/p&gt;  &lt;p&gt;Here's our updated example:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Scripting; &lt;span class="rem"&gt;//From Microsoft.Scripting.dll&lt;/span&gt;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Scripting.Hosting; &lt;span class="rem"&gt;//From Microsoft.Scripting.dll&lt;/span&gt;
&lt;span class="rem"&gt;//You'll also need to reference IronPython.dll&lt;/span&gt;
&lt;span class="rem"&gt;//and IronPython.Modules.dll&lt;/span&gt;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; Hosting2 {
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Host {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main() {
            &lt;span class="rem"&gt;//Get the IronPython ScriptEngine for the current domain&lt;/span&gt;
            &lt;span class="rem"&gt;//and create a ScriptModule to execute in.&lt;/span&gt;
            ScriptDomainManager sdm = ScriptDomainManager.CurrentManager;
            LanguageProvider pyprov = sdm.GetLanguageProvider(&lt;span class="str"&gt;&amp;quot;python&amp;quot;&lt;/span&gt;);
            ScriptEngine pe = pyprov.GetEngine();
            ScriptModule mod = sdm.CreateModule(&lt;span class="str"&gt;&amp;quot;mymod&amp;quot;&lt;/span&gt;);


            &lt;span class="rem"&gt;//Expose host data to IronPython and vice versa&lt;/span&gt;
            mod.SetVariable(&lt;span class="str"&gt;&amp;quot;mystring&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;This is a C# string&amp;quot;&lt;/span&gt;);
            pe.Execute(&lt;span class="str"&gt;&amp;quot;print mystring&amp;quot;&lt;/span&gt;, mod);

            pe.Execute(&lt;span class="str"&gt;&amp;quot;mystring = 'This is a Python string'&amp;quot;&lt;/span&gt;, mod);
            Console.WriteLine(mod.LookupVariable(&lt;span class="str"&gt;&amp;quot;mystring&amp;quot;&lt;/span&gt;));


            &lt;span class="rem"&gt;//Expose a host method to IronPython and vice versa&lt;/span&gt;
            HostAPI myapi = &lt;span class="kwrd"&gt;new&lt;/span&gt; HostAPI();
            mod.SetVariable(&lt;span class="str"&gt;&amp;quot;api&amp;quot;&lt;/span&gt;, myapi);
            pe.Execute(&lt;span class="str"&gt;&amp;quot;api.Method()&amp;quot;&lt;/span&gt;, mod);

            pe.Execute(&lt;span class="str"&gt;@&amp;quot;def func():
    print 'This is a Python function'
&amp;quot;&lt;/span&gt;, mod);
            pe.Evaluate(&lt;span class="str"&gt;&amp;quot;func()&amp;quot;&lt;/span&gt;, mod);


            &lt;span class="rem"&gt;//Use delegates over the same two methods&lt;/span&gt;
            HostAPI.Del hostdel = &lt;span class="kwrd"&gt;new&lt;/span&gt; HostAPI.Del(myapi.Method);
            mod.SetVariable(&lt;span class="str"&gt;&amp;quot;hdel&amp;quot;&lt;/span&gt;, hostdel);
            pe.Execute(&lt;span class="str"&gt;&amp;quot;hdel()&amp;quot;&lt;/span&gt;, mod);

            HostAPI.Del ipydel = pe.EvaluateAs&amp;lt;HostAPI.Del&amp;gt;(&lt;span class="str"&gt;&amp;quot;func&amp;quot;&lt;/span&gt;,
                mod);
            ipydel();
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; HostAPI {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Del();

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Method() {
            Console.WriteLine(&lt;span class="str"&gt;&amp;quot;This is a host method&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/pre&gt;

&lt;br /&gt;

&lt;h4&gt;What's changed and why?&lt;/h4&gt;

&lt;p&gt;We've switched from using IronPython.Hosting to using Microsoft.Scripting.Hosting.&amp;#160; Microsoft.Scripting is the primary namespace of the DLR, which 2.0 builds of IronPython are based on.&lt;/p&gt;

&lt;p&gt;The DLR's hosting model is designed to support many different dynamic languages.&amp;#160; So instead of simply constructing a PythonEngine as we did in 1.1, we ask the DLR to provide us with the ScriptEngine mapped to the language &amp;quot;python&amp;quot; in this AppDomain.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            ScriptDomainManager sdm = ScriptDomainManager.CurrentManager;
            LanguageProvider pyprov = sdm.GetLanguageProvider(&lt;span class="str"&gt;&amp;quot;python&amp;quot;&lt;/span&gt;);
            ScriptEngine pe = pyprov.GetEngine();&lt;/pre&gt;

&lt;p&gt;In the process we've introduced two entirely new types: ScriptDomainManager and LanguageProvider.&amp;#160; Every AppDomain in a DLR host has exactly one ScriptDomainManager.&amp;#160; Among other things, ScriptDomainManager tracks which languages are available on the system.&amp;#160; In this case we've asked the ScriptDomainManager to give us the LanguageProvider mapping to Python.&amp;#160; If we had an appropriate version of &lt;a href="http://www.ironruby.net/"&gt;IronRuby&lt;/a&gt; installed, we could ask for Ruby in exactly the same manner.&amp;#160; We use the LanguageProvider to give us language-specific services including the ScriptEngine.&lt;/p&gt;

&lt;p&gt;For an execution scope, we're using a ScriptModule instead of the EngineModule type we used in 1.1.&amp;#160; Accessing the contents of a ScriptModule is a little different but shouldn't be too jarring.&lt;/p&gt;

&lt;p&gt;Lastly, in 1.1 we were able to create a delegate to IronPython code by calling PythonEngine.CreateMethod with a string of code to wrap in a delegate.&amp;#160; That method no longer exists in 2.0.&amp;#160; But we can still get a delegate to an IronPython function by evaluating a function as our target delegate.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            HostAPI.Del ipydel = pe.EvaluateAs&amp;lt;HostAPI.Del&amp;gt;(&lt;span class="str"&gt;&amp;quot;func&amp;quot;&lt;/span&gt;,
                mod);
            ipydel();&lt;/pre&gt;

&lt;p&gt;And actually, this same code snippet works in 1.1 as well.&lt;/p&gt;

&lt;br /&gt;

&lt;h4&gt;But you could have done...&lt;/h4&gt;

&lt;p&gt;Some of you may note that there are several ways I could've written this example for 2.0 Alpha 6, and many of them are simpler than the one I chose.&amp;#160; For instance, this one line of code:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            ScriptEngine pe = IronPython.Hosting.PythonEngine.CurrentEngine;&lt;/pre&gt;

&lt;p&gt;Is equivalent to these three lines:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            ScriptDomainManager sdm = ScriptDomainManager.CurrentManager;
            LanguageProvider pyprov = sdm.GetLanguageProvider(&lt;span class="str"&gt;&amp;quot;python&amp;quot;&lt;/span&gt;);
            ScriptEngine pe = pyprov.GetEngine();&lt;/pre&gt;

&lt;p&gt;I chose the latter to provide a more language independent view of the API, which is a central driving force behind our redesigns.&lt;/p&gt;

&lt;p&gt;Furthermore, for this very simple example we could've used the Microsoft.Scripting.Script class.&amp;#160; Consider the following complete program:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Scripting;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; Hosting2 {
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Host {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main() {
            Script.SetVariable(&lt;span class="str"&gt;&amp;quot;mystring&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;This is a C# string&amp;quot;&lt;/span&gt;);
            Script.Execute(&lt;span class="str"&gt;&amp;quot;python&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;print mystring&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/pre&gt;

&lt;p&gt;The Script class is intended as an entry level hosting introduction and doesn't allow things like executing in a non-default ScriptModule.&lt;/p&gt;

&lt;br /&gt;

&lt;h4&gt;Next...&lt;/h4&gt;

&lt;p&gt;In this post we've looked at the hosting model of 2.0 Alpha 6 and some ways it differs from the model of 1.1.&amp;#160; Next time we'll look at the (hopefully) final hosting model for the DLR and talk more about the design evolution.&lt;/p&gt;
&lt;style type="text/css"&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: white;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: black;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: lime; }
.csharpcode .kwrd { color: #ff8000; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6603455" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/rdawson/archive/tags/DLR/">DLR</category><category domain="http://blogs.msdn.com/b/rdawson/archive/tags/IronPython/">IronPython</category><category domain="http://blogs.msdn.com/b/rdawson/archive/tags/Hosting/">Hosting</category></item><item><title>Hello World</title><link>http://blogs.msdn.com/b/rdawson/archive/2007/11/28/hello-world.aspx</link><pubDate>Wed, 28 Nov 2007 23:29:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6584376</guid><dc:creator>rdawson</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/rdawson/rsscomments.aspx?WeblogPostID=6584376</wfw:commentRss><comments>http://blogs.msdn.com/b/rdawson/archive/2007/11/28/hello-world.aspx#comments</comments><description>&lt;p&gt;Hello everyone.&amp;#160; Welcome to my blog.&amp;#160; The focus of this blog will be dynamic languages at Microsoft and specifically the &lt;a href="http://blogs.msdn.com/hugunin/archive/2007/04/30/a-dynamic-language-runtime-dlr.aspx"&gt;Dynamic Language Runtime&lt;/a&gt;, &lt;a href="http://codeplex.com/IronPython"&gt;IronPython&lt;/a&gt;, and &lt;a href="http://www.ironruby.net/"&gt;IronRuby&lt;/a&gt;.&amp;#160; I'll be talking about the design of various features as they take shape and how we're testing everything.&lt;/p&gt;  &lt;p&gt;First, a little about me.&amp;#160; I'm new to blogging (be gentle), but not new to MS where I've been a &amp;quot;Software Design Engineer in Test&amp;quot;, SDET, for nearly 9 years.&amp;#160; The first 7 of those years were spent on the Common Language Runtime, CLR, team where I worked on a variety of features over time including MS-IL, P/Invoke, and delegates.&lt;/p&gt;  &lt;p&gt;Presently I'm a member of the Dynamic Language Runtime, DLR, team working to improve support for dynamic languages in .NET as well as to provide first-class language implementations of Ruby and Python.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6584376" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/rdawson/archive/tags/Life/">Life</category></item><item><title>Hosting IronPython 1.1</title><link>http://blogs.msdn.com/b/rdawson/archive/2007/11/28/hosting-ironpython-1-1.aspx</link><pubDate>Wed, 28 Nov 2007 23:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6584380</guid><dc:creator>rdawson</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/rdawson/rsscomments.aspx?WeblogPostID=6584380</wfw:commentRss><comments>http://blogs.msdn.com/b/rdawson/archive/2007/11/28/hosting-ironpython-1-1.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;A recurring topic on the &lt;a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" mce_href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com"&gt;IronPython mailing list&lt;/a&gt; is the subject of hosting IPy in another application.&amp;#160; Part of the reason the topic comes up frequently is that we keep changing the design (we're almost done doing that...we hope).&lt;/p&gt;  &lt;p&gt;I thought it might be helpful to discuss where our hosting design has been, where it is, and where it's going within in the context of one consistent example.&amp;#160; To that end this is the first in a series of three posts that will focus on one host showing the following basic concepts:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Executing multiple statements of IronPython code in a single scope &lt;/li&gt;    &lt;li&gt;Exposing host data to IronPython, and vice versa &lt;/li&gt;    &lt;li&gt;Calling a host method in IronPython, and vice versa &lt;/li&gt;    &lt;li&gt;Using delegates &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I'll start by discussing IronPython 1.1 in this entry, current 2.0 Alphas in the next, and finish with the model coming in future 2.0 Alphas (likely &amp;gt;7). &lt;/p&gt;  &lt;p mce_keep="true"&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=2573" mce_href="http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=2573"&gt;&lt;strong&gt;IronPython 1.1&lt;/strong&gt;&lt;/a&gt; is the latest production version.&amp;#160; Released back in April 2007, this is the last version of IronPython not to be based on the Dynamic Language Runtime.&amp;#160; Meaning, this hosting model was designed primarily to support the hosting of IronPython only, and there is no Microsoft.Scripting.dll.&lt;/p&gt;  &lt;p&gt;Here's our example host in C#:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; IronPython.Hosting; &lt;span class="rem"&gt;//From IronPython.dll&lt;/span&gt;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; Hosting1 {
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Host {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main() {
            &lt;span class="rem"&gt;//Create a PythonEngine to execute code&lt;/span&gt;
            &lt;span class="rem"&gt;//and an EngineModule to execute in.&lt;/span&gt;
            PythonEngine pe = &lt;span class="kwrd"&gt;new&lt;/span&gt; PythonEngine();
            EngineModule mod = pe.CreateModule(&lt;span class="str"&gt;&amp;quot;mymod&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&gt;);


            &lt;span class="rem"&gt;//Expose host data to IronPython and vice versa&lt;/span&gt;
            mod.Globals[&lt;span class="str"&gt;&amp;quot;mystring&amp;quot;&lt;/span&gt;] = &lt;span class="str"&gt;&amp;quot;This is a C# string&amp;quot;&lt;/span&gt;;
            pe.Execute(&lt;span class="str"&gt;&amp;quot;print mystring&amp;quot;&lt;/span&gt;, mod);

            pe.Execute(&lt;span class="str"&gt;&amp;quot;mystring = 'This is a Python string'&amp;quot;&lt;/span&gt;, mod);
            Console.WriteLine(mod.Globals[&lt;span class="str"&gt;&amp;quot;mystring&amp;quot;&lt;/span&gt;]);


            &lt;span class="rem"&gt;//Expose a host method to IronPython and vice versa&lt;/span&gt;
            HostAPI myapi = &lt;span class="kwrd"&gt;new&lt;/span&gt; HostAPI();
            mod.Globals[&lt;span class="str"&gt;&amp;quot;api&amp;quot;&lt;/span&gt;] = myapi;
            pe.Execute(&lt;span class="str"&gt;&amp;quot;api.Method()&amp;quot;&lt;/span&gt;, mod);

            pe.Execute(&lt;span class="str"&gt;@&amp;quot;def func():
    print 'This is a Python function'
&amp;quot;&lt;/span&gt;, mod);
            pe.Evaluate(&lt;span class="str"&gt;&amp;quot;func()&amp;quot;&lt;/span&gt;, mod);


            &lt;span class="rem"&gt;//Use delegates over the same two methods&lt;/span&gt;
            HostAPI.Del hostdel = &lt;span class="kwrd"&gt;new&lt;/span&gt; HostAPI.Del(myapi.Method);
            mod.Globals[&lt;span class="str"&gt;&amp;quot;hdel&amp;quot;&lt;/span&gt;] = hostdel;
            pe.Execute(&lt;span class="str"&gt;&amp;quot;hdel()&amp;quot;&lt;/span&gt;, mod);

            HostAPI.Del ipydel = pe.CreateMethod&amp;lt;HostAPI.Del&amp;gt;(&lt;span class="str"&gt;&amp;quot;func()&amp;quot;&lt;/span&gt;,&lt;br /&gt;                mod);
            ipydel();
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; HostAPI {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Del();

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Method() {
            Console.WriteLine(&lt;span class="str"&gt;&amp;quot;This is a host method&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/pre&gt;

&lt;p mce_keep="true"&gt;&amp;#160;&lt;/p&gt;

&lt;h4&gt;PythonEngine and EngineModule&lt;/h4&gt;

&lt;p&gt;Now let's dig a little deeper.&amp;#160; We've used only two types from the hosting APIs: PythonEngine, and EngineModule.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            PythonEngine pe = &lt;span class="kwrd"&gt;new&lt;/span&gt; PythonEngine();
            EngineModule mod = pe.CreateModule(&lt;span class="str"&gt;&amp;quot;mymod&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&gt;);&lt;/pre&gt;

&lt;p&gt;The PythonEngine is the primary entry point for code execution.&amp;#160; The EngineModule is a scope in which code gets executed.&amp;#160; PythonEngine exposes a DefaultModule, which you can use instead of creating your own EngineModule.&amp;#160; You will notice that PythonEngine.Execute and PythonEngine.Evaluate methods have overloads that do not take an EngineModule.&amp;#160; These overloads work against the DefaultModule.&lt;/p&gt;

&lt;p&gt;EngineModule exposes a dictionary of defined names via its Globals property, which both host and scripts have read/write access to.&amp;#160; The host is free to insert any .NET type into the dictionary, which then exposes that type to IronPython script code.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            mod.Globals[&lt;span class="str"&gt;&amp;quot;api&amp;quot;&lt;/span&gt;] = myapi;&lt;/pre&gt;

&lt;p mce_keep="true"&gt;&amp;#160;&lt;/p&gt;

&lt;h4&gt;Delegates&lt;/h4&gt;

&lt;p&gt;The last major concept here is the usage of delegates.&amp;#160; .NET delegates are directly callable from IronPython.&amp;#160; No special magic is required on either the host or script's part.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            HostAPI.Del hostdel = &lt;span class="kwrd"&gt;new&lt;/span&gt; HostAPI.Del(myapi.Method);
            mod.Globals[&lt;span class="str"&gt;&amp;quot;hdel&amp;quot;&lt;/span&gt;] = hostdel;
            pe.Execute(&lt;span class="str"&gt;&amp;quot;hdel()&amp;quot;&lt;/span&gt;, mod);&lt;/pre&gt;

&lt;p&gt;In the reverse, you can get a delegate to IronPython code in 1.1 via the PythonEngine.CreateMethod APIs.&amp;#160; Sometimes this is more convenient than calling PythonEngine.Evaluate or PythonEngine.Execute.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            HostAPI.Del ipydel = pe.CreateMethod&amp;lt;HostAPI.Del&amp;gt;(&lt;span class="str"&gt;&amp;quot;func()&amp;quot;&lt;/span&gt;,&lt;br /&gt;                mod);
            ipydel();&lt;/pre&gt;

&lt;p mce_keep="true"&gt;&amp;#160;&lt;/p&gt;

&lt;h4&gt;Next...&lt;/h4&gt;

&lt;p&gt;In my next entry I'll take this same example, re-write it for IronPython 2.0 Alpha 6, and discuss how and why things changed.&lt;/p&gt;

&lt;p&gt;In the meantime, if you'd like to read more about hosting IronPython, the &lt;a href="http://www.ironpython.info/index.php/Contents#Embedding" mce_href="http://www.ironpython.info/index.php/Contents#Embedding"&gt;Cookbook&lt;/a&gt; has some good examples.&amp;#160; And if there are any hosting concepts I haven't touched on that you wish I would, please let me know.&lt;/p&gt;

&lt;p&gt;&lt;style type="text/css"&gt;





















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: white;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: black;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: lime; }
.csharpcode .kwrd { color: #ff8000; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6584380" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/rdawson/archive/tags/IronPython/">IronPython</category><category domain="http://blogs.msdn.com/b/rdawson/archive/tags/Hosting/">Hosting</category></item></channel></rss>