<?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>Srivatsn's Blog</title><link>http://blogs.msdn.com/b/srivatsn/</link><description>Visual Basic .NET, IronPython and the DLR</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Visual Basic comes to Windows Phone!</title><link>http://blogs.msdn.com/b/srivatsn/archive/2010/09/23/visual-basic-comes-to-windows-phone.aspx</link><pubDate>Thu, 23 Sep 2010 17:16:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10066873</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=10066873</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2010/09/23/visual-basic-comes-to-windows-phone.aspx#comments</comments><description>&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Ah.. It’s such a relief to be able to finally talk about it &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-51-75-metablogapi/4810.wlEmoticonsmile_5F00_38CC37C8.png" /&gt; I’ve been working on getting Visual Basic supported in the Windows Phone development tools. It’s been hard to not talk about it, especially when I’d seen comments from users like “The phone dev tools are great but it’s sad that there is no VB support”.. You can read about the announcement here on &lt;/font&gt;&lt;a href="http://blogs.msdn.com/b/somasegar/archive/2010/09/23/windows-phone-vb-developers-great-mobile-apps.aspx"&gt;&lt;font size="3" face="Calibri"&gt;Soma’s blog&lt;/font&gt;&lt;/a&gt;&lt;font size="3" face="Calibri"&gt;. Jason Zander has a &lt;/font&gt;&lt;a href="http://blogs.msdn.com/b/jasonz/archive/2010/09/23/placeholder1.aspx"&gt;&lt;font size="3" face="Calibri"&gt;post showing&lt;/font&gt;&lt;/a&gt;&lt;font size="3" face="Calibri"&gt; a walkthrough of a basic VB windows phone application. The VB Team blog has a more &lt;/font&gt;&lt;a href="http://blogs.msdn.com/b/vbteam/archive/2010/09/23/announcing-microsoft-visual-basic-ctp-for-windows-phone-developer-tools.aspx"&gt;&lt;font size="3" face="Calibri"&gt;detailed walkthrough&lt;/font&gt;&lt;/a&gt;&lt;font size="3" face="Calibri"&gt;. I’ll post about the details of the release later. Have fun developing VB apps for the phone!&lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10066873" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/Phone/">Phone</category><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/VB/">VB</category></item><item><title>Solution Navigator: Get rid of your toolwindows</title><link>http://blogs.msdn.com/b/srivatsn/archive/2010/07/21/solution-navigator-get-rid-of-your-toolwindows.aspx</link><pubDate>Wed, 21 Jul 2010 00:37:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10040688</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=10040688</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2010/07/21/solution-navigator-get-rid-of-your-toolwindows.aspx#comments</comments><description>&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Adrian Collier has just &lt;/font&gt;&lt;a href="http://blogs.msdn.com/b/visualstudio/archive/2010/07/20/solution-navigator-blog-post.aspx"&gt;&lt;font size="3" face="Calibri"&gt;blogged about the release of the Solution Navigator extension for VS 2010&lt;/font&gt;&lt;/a&gt;&lt;font size="3" face="Calibri"&gt;. One of the things I hate about Visual Studio is the number of tool windows that I have to juggle around. I have two big monitors at work and I still run out of screen real estate every so often. I hate working from home because on my laptop screen, after all the toolwindows, there’s like seven pixels left for actual code. So, anything that promises to reduce the need for multiple toolwindows excites me. Solution navigator, in one fell swoop gets rid of a bunch of toolwindows that I use regularly. There is a solution navigator pane that takes the place of solution explorer and makes it much richer. The tooltips (pinnable!) are the killer feature for me though, hover over any artifact in your code and everything you’d want to know about that is present in a tooltip. &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;This works for both C# and VB projects. I also happened to write the initial support for VB projects before the Solution Navigator team took over ( It was a very fun week writing the basic plumbing and seeing everything light up!).&amp;#160; Check it out and give the team your feedback.&lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10040688" width="1" height="1"&gt;</description></item><item><title>Relax! so what if the return type is missing?</title><link>http://blogs.msdn.com/b/srivatsn/archive/2010/07/14/relax-so-what-if-the-return-type-is-missing.aspx</link><pubDate>Wed, 14 Jul 2010 04:18:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10037921</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=10037921</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2010/07/14/relax-so-what-if-the-return-type-is-missing.aspx#comments</comments><description>&lt;div align="justify"&gt;   &lt;p&gt;&lt;font size="3" face="Calibri"&gt;Visual Basic generally goes out of its way to help out the programmer. Delegate relaxation is a good example of this need to please. Simply put, this feature is about being able to assign to delegates when the signatures don’t exactly match. The canonical example goes like this:&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;Sub &lt;/span&gt;ClickHandler() &lt;span style="color: blue"&gt;Handles &lt;/span&gt;Button1.Click&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="3" face="Calibri"&gt;Button1.Click takes two parameters whereas ClickHandler takes no parameters. This is quite a convenience if you don’t plan to use the eventargs.&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="3" face="Calibri"&gt;Another example that’s easier to examine is something like:&lt;/font&gt;&lt;/p&gt;    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;a &lt;span style="color: blue"&gt;As &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;= &lt;span style="color: blue"&gt;AddressOf String&lt;/span&gt;.Empty.ToString&lt;/pre&gt;

  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;Action is a delegate that takes no parameters and has no return type. We are assigning a function to it which has a return type of String. Here is the code that’s generated for this case.&lt;/font&gt;&lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;$VB$Closure_ClosureVariable_14_D &lt;span style="color: blue"&gt;As New &lt;/span&gt;_Closure$__1
$VB$Closure_ClosureVariable_14_D.$VB$Local_VB$t_string$S1 = &lt;span style="color: blue"&gt;String&lt;/span&gt;.Empty
&lt;span style="color: blue"&gt;Dim &lt;/span&gt;a &lt;span style="color: blue"&gt;As &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;= &lt;span style="color: blue"&gt;New &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;(&lt;span style="color: blue"&gt;AddressOf &lt;/span&gt;$VB$Closure_ClosureVariable_14_D._Lambda$__1)

&lt;span style="color: blue"&gt;Public Sub &lt;/span&gt;_Lambda$__1()
&lt;span style="color: blue"&gt;&lt;font color="#333333"&gt;    &lt;/font&gt;Me&lt;/span&gt;.$VB$Local_VB$t_string$S1.ToString
&lt;span style="color: blue"&gt;End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;What is all this messy code doing? What happens is that when the compiler sees a delegate of type &amp;lt;expr&amp;gt;.method it breaks it down into temp = &amp;lt;expr&amp;gt; and temp.Method. The temp var is &lt;/font&gt;&lt;font size="3" face="Calibri"&gt;then hoisted into a closure if needed. The closure class also has a method which has the right signature needed for the target delegate. This function simply calls temp.Method. If there was no relaxation needed (meaning no lambda is generated) then the temp is simply a temp and doesn’t have to be hoisted. &lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10037921" width="1" height="1"&gt;</description></item><item><title>How I landed up in the Visual Basic team.</title><link>http://blogs.msdn.com/b/srivatsn/archive/2010/07/11/how-i-landed-up-in-the-visual-basic-team.aspx</link><pubDate>Sun, 11 Jul 2010 01:27:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10036818</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=10036818</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2010/07/11/how-i-landed-up-in-the-visual-basic-team.aspx#comments</comments><description>&lt;p&gt;&lt;font size="2"&gt;My last post was in September 2008. It’s been close to two years since I posted anything. My rationalization for such unforgivable behaviour is that I started working on a project that wasn’t public at that time. This was an incubation effort and I was part of a very small team of fantastic people that adopted agile practices and is easily one of the best teams for which I’ve worked. After that effort reached it’s conclusion, I, changed roles and moved to the Visual Basic .NET team as a developer and during the Dev10 cycle I owned intellisense, the expression editor and even code model for a while. Recently I’ve started working more with the compiler codebase and there is a quite a bit of blog fodder when you work in the compiler space. So I’m reviving this blog to write about all the corner cases of the language that I’m learning – or atleast that is the reason I give myself for doing something that I should just have been doing all along for the past two years.&lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10036818" width="1" height="1"&gt;</description></item><item><title>Hosting IronPython made easier</title><link>http://blogs.msdn.com/b/srivatsn/archive/2008/09/16/hosting-ironpython-made-easier.aspx</link><pubDate>Wed, 17 Sep 2008 01:30:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8954481</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=8954481</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2008/09/16/hosting-ironpython-made-easier.aspx#comments</comments><description>&lt;p&gt;With 2.0 Beta 5 coming out very soon, there is a new hosting helper class in there called IronPython.Hosting.Python . It has a few helper methods to create a ScriptRuntime or ScriptEngine and adds some Python-specific extension methods to ScriptRuntime and ScriptEngine.&lt;/p&gt;  &lt;p&gt;Creating a ScriptRuntime the normal way now requires a LanguageSetup as Sesh &lt;a href="http://blogs.msdn.com/seshadripv/archive/2008/09/07/dlr-hosting-api-latest-version-of-the-spec-is-available-online-includes-changes-to-runtime-initialization.aspx"&gt;explains here&lt;/a&gt;. The Python class has helpers called CreateRuntimeSetup and CreateLanguageSetup which return a pythonic ScriptRuntimeSetup and LanguageSetup&amp;#160; respectively. But if you are not interested in the configuration options of the DLR then you can simply use Python.CreateEngine/ Python.CreateRuntime.&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ScriptRuntime &lt;/span&gt;runtime = &lt;span style="color: #2b91af"&gt;Python&lt;/span&gt;.CreateRuntime();
runtime.ExecuteFile(&lt;span style="color: #a31515"&gt;&amp;quot;foo.py&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are also three extension methods tacked on to ScriptRuntime and ScriptEngine called GetSysModule, GetBuiltinModule and GetClrModule which directly return a ScriptScope for these modules. You can do something like this for example:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;IronPython.Hosting;

&lt;span style="color: #2b91af"&gt;ScriptEngine &lt;/span&gt;engine = &lt;span style="color: #2b91af"&gt;Python&lt;/span&gt;.CreateEngine();

&lt;span style="color: #2b91af"&gt;ScriptScope &lt;/span&gt;sys = engine.GetSysModule();
&lt;span style="color: blue"&gt;var &lt;/span&gt;platform = sys.GetVariable(&lt;span style="color: #a31515"&gt;&amp;quot;platform&amp;quot;&lt;/span&gt;);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(platform);

&lt;span style="color: #2b91af"&gt;ScriptScope &lt;/span&gt;builtins = engine.GetBuiltinModule();
&lt;span style="color: blue"&gt;var &lt;/span&gt;pow = builtins.GetVariable&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;,&lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;&amp;gt;(&lt;span style="color: #a31515"&gt;&amp;quot;pow&amp;quot;&lt;/span&gt;);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(pow(2,3));

&lt;span style="color: #2b91af"&gt;ScriptScope &lt;/span&gt;clr = engine.GetClrModule();
&lt;span style="color: blue"&gt;var &lt;/span&gt;getPythonType = clr.GetVariable&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;PythonType&lt;/span&gt;&amp;gt;&amp;gt;(&lt;span style="color: #a31515"&gt;&amp;quot;GetPythonType&amp;quot;&lt;/span&gt;);            
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #2b91af"&gt;PythonType&lt;/span&gt;.Get__name__(getPythonType(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;))));&lt;/pre&gt;
&lt;/blockquote&gt;
This will then print out cli, 8 and str respectively. 

&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8954481" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/DLR/">DLR</category><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/IronPython/">IronPython</category></item><item><title>Passing keyword args to C# methods from IronPython</title><link>http://blogs.msdn.com/b/srivatsn/archive/2008/09/09/passing-keyword-args-to-c-methods-from-ironpython.aspx</link><pubDate>Wed, 10 Sep 2008 00:13:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8937438</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=8937438</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2008/09/09/passing-keyword-args-to-c-methods-from-ironpython.aspx#comments</comments><description>&lt;p&gt;In Python one can call functions with either the argument itself or a named argument i.e f(3) or f(x=3). Python also provides syntax to absorb excess arguments for either style of argument passing with either * or **. So for example for this function f:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;def &lt;/span&gt;f(x, *args, **kwargs):
    &lt;span style="color: blue"&gt;pass
    
&lt;/span&gt;f(3,4,5,y=6,z=7)&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The value passed to x is 3 and args then absorbs the extra positional arguments and is passed as a tuple (4,5). kwargs then takes up the keyword arguments and is passed as a dictionary {'y':6, 'z':7}.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variable number of arguments in the CLS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now if 'f' were a C# method would this still work? Passing the positional and named parameters from IronPython definitely works. The question is can C# accept variable number of parameters? The &lt;a href="http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx"&gt;CLS&lt;/a&gt; clearly states that the only calling convention supported is the standard managed calling convention and variable length argument lists are not allowed. So how does C# support the params keyword then? The C# compiler applies an attribute (&lt;a href="http://msdn.microsoft.com/en-us/library/system.paramarrayattribute.aspx"&gt;System.ParamArray&lt;/a&gt;) and passes the extra positional parameters as an array. Great, so can we then do the same thing for dictionary params as well? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ParamDictionaryAttribute&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There is a ParamDictionaryAttribute in Microsoft.Scripting.dll that serves the purpose of the System.ParamArray for keyword arguments. But since the C# compiler doesn't know about it there is no syntactic sugar here and it needs to be manually applied. We can, therefore, define a function like this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue"&gt;using &lt;/span&gt;Microsoft.Scripting;

&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;foo &lt;/span&gt;{
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;bar(&lt;span style="color: blue"&gt;int &lt;/span&gt;x, [&lt;span style="color: #2b91af"&gt;ParamDictionary&lt;/span&gt;] &lt;span style="color: #2b91af"&gt;IAttributesCollection &lt;/span&gt;kwargs) {
        &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SymbolId &lt;/span&gt;key &lt;span style="color: blue"&gt;in &lt;/span&gt;kwargs.SymbolAttributes.Keys) {
            &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;key: {0}, value: {1} &amp;quot;&lt;/span&gt;, key, kwargs[key]);
        }
    }
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;font face="Verdana"&gt;The function bar now accepts a dictionary of excess keyword parameters when called from IronPython. IAttributesCollection is another interface defined in Microsoft.Scripting which represents a dictionary that can be accessed using SymbolIds and also arbitrary objects. The SymbolAttributes property returns just the SymbolIds. This can now be called from IronPython like so:&lt;/font&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;import &lt;/span&gt;clr
clr.AddReference(&lt;span style="color: maroon"&gt;&amp;quot;test.dll&amp;quot;&lt;/span&gt;)
&lt;span style="color: blue"&gt;import &lt;/span&gt;foo
foo().bar(2,y=4, z=6)&lt;/pre&gt;
&lt;/blockquote&gt;
And you'll see y:4 and z:6 were indeed passed into the method as items in the dictionary.&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8937438" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/DLR/">DLR</category><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/IronPython/">IronPython</category></item><item><title>Static Compilation of IronPython scripts</title><link>http://blogs.msdn.com/b/srivatsn/archive/2008/08/06/static-compilation-of-ironpython-scripts.aspx</link><pubDate>Wed, 06 Aug 2008 09:28:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8836253</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=8836253</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2008/08/06/static-compilation-of-ironpython-scripts.aspx#comments</comments><description>&lt;p&gt;The ability to compile IronPython scripts into .NET IL and to save them to disk existed in IronPython 1.0 but has been missing in 2.0 so far. With IronPython 2.0 Beta4 this has been added back. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Why would I compile dynamic language scripts?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;There are a lot of reasons to compile scripts into a binary form. Shri talks about some of them &lt;a href="http://blogs.msdn.com/shrib/archive/2008/07/24/cls-compilation-of-ironpython.aspx"&gt;here&lt;/a&gt;. For folks who don't want to distribute source code in plain text this provides one level of obfuscation. To that end, a function called CompileModules has been added to the clr module to compile scripts into executable IL. The signature of the function is:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;CompileModules(str assemblyName, dict kwArgs, &lt;span style="color: #2b91af"&gt;&lt;font color="#000000"&gt;Array&lt;/font&gt;&lt;/span&gt;[str] filenames)&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;So to compile a file foo.py into foo.dll you would do this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;import clr
clr.CompileModules(&amp;quot;foo.dll&amp;quot;, &amp;quot;foo.py&amp;quot;)&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This can now be brought in using the regular clr.AddReference. When clr.AddReference sees a compiled assembly, it publishes the module as well. So one can simply import the module into the code.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;clr.AddReference(&amp;quot;foo.dll&amp;quot;)
import foo&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;Multiple files and main&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The function can take multiple python files and compile them into one dll. What if you want it to be a standalone executable? There are two things to be done. First, a stub exe is needed that can load the dll. Second, a way to distinguish the main module is needed. The keyword args that CompileModules can take comes in handy here&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;import clr
clr.CompileModules(&amp;quot;foo.dll&amp;quot;, &amp;quot;foo.py&amp;quot;, &amp;quot;bar.py&amp;quot;, mainModule=&amp;quot;main.py&amp;quot;)&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now a stub exe can be written that loads up this compiled dll. The IronPython sample pyc.py has code that does shows how to generate a stub exe.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wait, what is -X:SaveAssemblies mode then?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When IronPython is started with -X:SaveAssemblies, it generates a dll containing IL corresponding to the code it executed. Sounds an awful lot like compilation doesn't it? The difference is one is executable IL and the other isn't.&lt;/p&gt;

&lt;p&gt; To understand the difference, one needs to understand that IronPython under normal course of its execution generates IL anyway. Every statement is converted to the DLR AST and IL gets spit out for the ASTs which is then executed. The SaveAssemblies mode simply dumps the generated IL into a dll. It is meant as a debugging device. So what is missing from this IL that prevents it from being re-executable code? The short answer is Dynamic Sites. The sites that are generated during the execution are not persisted. The compilation feature does exactly this - it persists the dynamic sites as well. Lets look at an example here and compare the generated IL in reflector. (Only the relevant code is copied over from reflector). This python code:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;print 2 + 5
print 2 * 5
print 3 / 5&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;when run with -X:SaveAssemblies mode produces this code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; #Constant207;
&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; #Constant208;
&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; #Constant209;&lt;/pre&gt;

&lt;pre class="code"&gt;$lineNo = 1;
&lt;span style="color: #2b91af"&gt;PythonOps&lt;/span&gt;.Print(__global_context, #Constant207.Target(#Constant207, 2, 5));
$lineNo = 2;
&lt;span style="color: #2b91af"&gt;PythonOps&lt;/span&gt;.Print(__global_context, #Constant208.Target(#Constant208, 2, 5));
$lineNo = 3;
&lt;span style="color: #2b91af"&gt;PythonOps&lt;/span&gt;.Print(__global_context, #Constant209.Target(#Constant209, 3, 5));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice that the Constants defined here are actually defined as fields on the generated type and this type doesn't get instantiated anywhere and therefore the sites don't get assigned anywhere. The same python code when compiled with clr.CompiledModules produces this code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;object&lt;/span&gt;[] objArray = &lt;span style="color: blue"&gt;new object&lt;/span&gt;[] { 
&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;.Create(&lt;span style="color: #2b91af"&gt;PythonOps&lt;/span&gt;.MakeOperationAction(context, &lt;span style="color: #a31515"&gt;&amp;quot;Add&amp;quot;&lt;/span&gt;)), 
&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;.Create(&lt;span style="color: #2b91af"&gt;PythonOps&lt;/span&gt;.MakeOperationAction(context, &lt;span style="color: #a31515"&gt;&amp;quot;Multiply&amp;quot;&lt;/span&gt;)), 
&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;.Create(&lt;span style="color: #2b91af"&gt;PythonOps&lt;/span&gt;.MakeOperationAction(context, &lt;span style="color: #a31515"&gt;&amp;quot;Divide&amp;quot;&lt;/span&gt;)) 
};&lt;/pre&gt;

&lt;pre class="code"&gt;line = 1;
&lt;span style="color: #2b91af"&gt;PythonOps&lt;/span&gt;.Print(context, ((&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;)objArray[0]).Target(
    (&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;)objArray[0], 2, 5));
line = 2;
&lt;span style="color: #2b91af"&gt;PythonOps&lt;/span&gt;.Print(context, ((&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;)objArray[1]).Target(
    (&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;)objArray[1], 2, 5));
line = 3;
&lt;span style="color: #2b91af"&gt;PythonOps&lt;/span&gt;.Print(context, ((&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;)objArray[2]).Target(
    (&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DynamicSiteTarget&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;)objArray[2], 3, 5));&lt;/pre&gt;

&lt;p&gt;You can see that all the dynamic call sites are being created here and their targets are being invoked. This then is perfectly executable code - maybe not as succinct as the python code but it does the same thing :)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8836253" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/DLR/">DLR</category><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/IronPython/">IronPython</category></item><item><title>IronPython 2.0 Beta 3 is out!</title><link>http://blogs.msdn.com/b/srivatsn/archive/2008/06/13/ironpython-2-0-beta-3-is-out.aspx</link><pubDate>Sat, 14 Jun 2008 01:58:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8595112</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=8595112</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2008/06/13/ironpython-2-0-beta-3-is-out.aspx#comments</comments><description>&lt;p&gt;The next stop on the path to IronPython 2.0 is Beta 3 and we pushed it out today. You can get the sources and binaries of IronPython &lt;a href="http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=12988"&gt;here&lt;/a&gt;. As Jimmy mentioned in &lt;a href="http://blog.jimmy.schementi.com/2008/06/dynamic-languages-in-silverlight-2-beta.html"&gt;his release notes&lt;/a&gt; for Dynamic Silverlight last week, this release also contains the sources and binaries of IronPython and Dynamic Silverlight needed to build Silverlight applications with IronPython. This release builds against &lt;a href="http://www.microsoft.com/silverlight/resources/install.aspx?v=2.0"&gt;Silverlight 2.0 Beta 2&lt;/a&gt; – so make sure that is installed. &lt;/p&gt;  &lt;p&gt;Head over to the &lt;a href="http://www.codeplex.com/IronPython/Wiki/View.aspx?title=v2.0%20Beta%203%20Release%20Notes&amp;amp;referringTitle=Home"&gt;release notes&lt;/a&gt; to find out what has changed since the last release and the list of bugs that have been fixed. Congratulations to the team and the community on the release.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8595112" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/IronPython/">IronPython</category><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/Silverlight/">Silverlight</category></item><item><title>Accessing IronPython objects from native Javascript</title><link>http://blogs.msdn.com/b/srivatsn/archive/2008/05/09/accessing-ironpython-objects-from-native-javascript.aspx</link><pubDate>Fri, 09 May 2008 10:16:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8478042</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=8478042</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2008/05/09/accessing-ironpython-objects-from-native-javascript.aspx#comments</comments><description>&lt;p&gt;Today I got a question by e-mail from someone who wanted to implement something like this &lt;a href="http://msdn.microsoft.com/en-us/library/a0746166.aspx"&gt;MSDN article&lt;/a&gt; in IronPython. The gist of the article is that there is a property called &lt;em&gt;ObjectForScripting&lt;/em&gt; on a WinForms WebBrowser control. If one sets the property to an object, then that object will be available to javascript that executes in the page through &lt;em&gt;window.external&lt;/em&gt;. The Form needs to add the ComVisible attribute.&lt;/p&gt;  &lt;p&gt;.NET attributes cannot be added to a class from IronPython but fortunately objects created are ComVisible by default. So we can try a direct translation of the code in the article :&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;import &lt;/span&gt;clr
clr.AddReference(&lt;span style="color: maroon"&gt;&amp;quot;System.Windows.Forms&amp;quot;&lt;/span&gt;)
&lt;span style="color: blue"&gt;from &lt;/span&gt;System.Windows.Forms &lt;span style="color: blue"&gt;import &lt;/span&gt;Application, Form, WebBrowser, MessageBox

&lt;span style="color: blue"&gt;class &lt;/span&gt;MyForm(Form):
    &lt;span style="color: blue"&gt;def &lt;/span&gt;__init__(self):
        self.wb = WebBrowser()
        self.Controls.Add(self.wb)
        self.wb.ObjectForScripting = self
        self.wb.DocumentText = &lt;span style="color: maroon"&gt;&amp;quot;&amp;quot;&amp;quot;
                               &lt;/span&gt;&lt;font color="#800000"&gt;&amp;lt;html&amp;gt;
                                &amp;lt;body&amp;gt;
                                 &amp;lt;button onclick=&amp;quot;window.external.Test('hello')&amp;quot;&amp;gt;
                                   call client code &lt;span style="color: blue"&gt;&lt;font color="#800000"&gt;from&lt;/font&gt; &lt;/span&gt;script code
                                 &amp;lt;/button&amp;gt;
                                &amp;lt;/body&amp;gt;
                               &amp;lt;/html&amp;gt;&lt;/font&gt;
                               &lt;span style="color: maroon"&gt;&amp;quot;&amp;quot;&amp;quot;
    &lt;/span&gt;&lt;span style="color: blue"&gt;def &lt;/span&gt;Test(self, msg):
        MessageBox.Show(msg)
    
        
Application.Run(MyForm())&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unfortunately this doesn't work because ObjectForScripting is being set to an instance of the python type MyForm. In the CLR-world this type will be generated dynamically and will be something like IronPython.NewTypes.System.Windows.Form_1$0 and will not contain any of the python methods. You can verify this by doing &lt;em&gt;clr.GetClrType(MyForm).GetMembers()&lt;/em&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;. The easiest solution then is to define an interface in C# with the methods that you want on your type and implement that interface on your type. So define a simple interface like this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IWebBrowserInterop
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;void &lt;/span&gt;Test(&lt;span style="color: blue"&gt;string &lt;/span&gt;message);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;and in the python code make your class implement that interface:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;from &lt;/span&gt;ClassLibrary1 &lt;span style="color: blue"&gt;import &lt;/span&gt;IWebBrowserInterop
&lt;font color="#0000ff"&gt;&lt;/font&gt;
&lt;span style="color: blue"&gt;class &lt;/span&gt;MyForm(Form, IWebBrowserInterop):
...&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now if you fire up the app and click on the button the MessageBox will popup. You just reached across from unmanaged javascript through COM/.NET to python&lt;/p&gt;

&lt;p&gt;Caveat(s):&amp;#160; This is not type-safe. The IronPython runtime caches the types that are generated. So if there is another class in the python code that is &amp;quot;similar&amp;quot; (same base classes/interfaces and not different __slots__-wise) then this type can be reused. So if you are not bound by other factors, you should try to use Managed JScript instead and the hosting APIs to interact with the python objects.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8478042" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/IronPython/">IronPython</category><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/Silverlight/">Silverlight</category></item><item><title>Turning your .NET object models dynamic for IronPython</title><link>http://blogs.msdn.com/b/srivatsn/archive/2008/04/12/turning-your-net-object-models-dynamic-for-ironpython.aspx</link><pubDate>Sat, 12 Apr 2008 05:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8383517</guid><dc:creator>Srivatsn N</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/srivatsn/rsscomments.aspx?WeblogPostID=8383517</wfw:commentRss><comments>http://blogs.msdn.com/b/srivatsn/archive/2008/04/12/turning-your-net-object-models-dynamic-for-ironpython.aspx#comments</comments><description>&lt;P&gt;Say you want to interop with a .NET library but you also want it to behave like objects in dynamic languages do. You want to be able to add/delete methods/properties to the object dynamically. In python you can do something like this:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;class &lt;/SPAN&gt;x(object):
    &lt;SPAN style="COLOR: blue"&gt;pass

&lt;/SPAN&gt;y = x()
y.z = 42
dir(y)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And the dir(y) will contain z in it. Now if&amp;nbsp;x were a .NET class&amp;nbsp;instead of a python class would you still be able to do the same? Let’s try a simple .NET&amp;nbsp;class&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TestExt
&lt;/SPAN&gt;{
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;From IronPython you would do something like this:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;import &lt;/SPAN&gt;clr
clr.AddReference(&lt;SPAN style="COLOR: maroon"&gt;"TestExtensions.dll"&lt;/SPAN&gt;)
&lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;TestExtensions &lt;SPAN style="COLOR: blue"&gt;import &lt;/SPAN&gt;TestExt
y = TestExt()
y.z = 42&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But this code throws a AttributeError saying ‘TestExt’ object has no attribute ‘z’. What now? Enter Stage Right DLR’s extension mechanism. There are five methods that a .NET class can implement that have a special meaning to the binder if you tell it so. The methods are:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;GetCustomMember&amp;nbsp;– runs before normal .NET lookups&lt;/LI&gt;
&lt;LI&gt;GetBoundMember&amp;nbsp;– runs after normal .NET lookups&lt;/LI&gt;
&lt;LI&gt;SetMember&amp;nbsp;– runs before normal .NET member assignment&lt;/LI&gt;
&lt;LI&gt;SetMemberAfter&amp;nbsp;– runs after normal .NET member assignment&lt;/LI&gt;
&lt;LI&gt;DeleteMember&amp;nbsp;– runs before normal .NET operator access (there’s no .NET version&amp;nbsp;– so its the only one)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;A .NET class can implement these functions and mark them with a &lt;A href="http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.specialnameattribute(VS.80).aspx"&gt;&lt;FONT color=#0000ff&gt;SpecialName attribute&lt;/FONT&gt;&lt;/A&gt;. The rule generated for binding now makes a call to Getter/Setter before and after the normal .NET binding is done. GetCustomMember/SetMember&amp;nbsp;&amp;nbsp;is called first and if it returns a value that is treated as the result of the member-lookup. This overrides any .NET members that may exist. But if you return OperationFailed.Value from the function, then it will proceed with the normal lookup as well. GetBoundMember/SetMemberAfter is called if the normal binding fails&amp;nbsp;– that is there is no&amp;nbsp;member of the name that it is trying to bind to. So with that in mind let’s modify the .NET class&amp;nbsp;and add&amp;nbsp;this:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Dictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt; dict = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Dictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;();
[&lt;SPAN style="COLOR: #2b91af"&gt;SpecialName&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public object &lt;/SPAN&gt;GetBoundMember(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;name)
{
    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(dict.ContainsKey(name))
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;dict[name];
    &lt;SPAN style="COLOR: blue"&gt;else
        return &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;OperationFailed&lt;/SPAN&gt;.Value;
}

[&lt;SPAN style="COLOR: #2b91af"&gt;SpecialName&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;SetMemberAfter(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;methodName, &lt;SPAN style="COLOR: blue"&gt;object &lt;/SPAN&gt;o)
{
    dict.Add(methodName, o);
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Now if I try to do a y.z = 42, it works. I can assign y.z to a function as well and will be able to call y.z(). I could have overridden GetCustomMember and SetMember instead and the behaviour&amp;nbsp;would be exactly the same since OperationFailed.Value is returned for member that are not in the dict but then there is this overhead involved for any .NET member lookup.&lt;/P&gt;
&lt;P&gt;The SetMember function can choose to return a bool instead of void and in that case, the returnvalue would control whether further binding lookups happen or not.&lt;/P&gt;
&lt;P&gt;So, what exactly is the use of all this, why would I want to do this? Consider writing an object model for something like&amp;nbsp;the following xml file:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;lt;foo&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bar&amp;gt;baz&amp;lt;/bar&amp;gt;&lt;BR&gt;&amp;lt;/foo&amp;gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Now I want to access this xml as foo.bar and the value of that should be baz. To implement this all that I would need to do is add the GetBoundMember method to the .NET XmlElement class to do a search and return&amp;nbsp;another XmlElement or add a extension method to XmlElement. Now here is the part where things come a little unstuck in IronPython. Extension methods dont show up in reflection, so the IronPython support for that currently isn’t where we want it to be. There is a way around though. You can decorate an assembly with an ExtensionType attribute describing what type you are extending and with what class. After that you would need to register the assembly once so that those methods get injected at the right places. This might change in the future with a better way but this works for now. This is the code you’d need to implement: 
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;[&lt;SPAN style="COLOR: blue"&gt;assembly&lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ExtensionType&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(System.Xml.&lt;SPAN style="COLOR: #2b91af"&gt;XmlElement&lt;/SPAN&gt;), &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(TestExtensions.&lt;SPAN style="COLOR: #2b91af"&gt;ExtClass&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #2b91af"&gt;XmlElementExtension&lt;/SPAN&gt;))]
&lt;SPAN style="COLOR: blue"&gt;namespace &lt;/SPAN&gt;TestExtensions
{    
    &lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExtClass
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;static &lt;/SPAN&gt;ExtClass()
        {
            Microsoft.Scripting.Runtime.&lt;SPAN style="COLOR: #2b91af"&gt;RuntimeHelpers&lt;/SPAN&gt;.RegisterAssembly(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;ExtClass&lt;/SPAN&gt;).Assembly);
        }

        &lt;SPAN style="COLOR: blue"&gt;public static &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XmlElement &lt;/SPAN&gt;Load(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;fileName)
        {
            &lt;SPAN style="COLOR: #2b91af"&gt;XmlDocument &lt;/SPAN&gt;doc = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XmlDocument&lt;/SPAN&gt;();
            doc.Load(fileName);
            &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;doc.DocumentElement;
        }
        &lt;SPAN style="COLOR: blue"&gt;public static class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XmlElementExtension
        &lt;/SPAN&gt;{
            [&lt;SPAN style="COLOR: #2b91af"&gt;SpecialName&lt;/SPAN&gt;]
            &lt;SPAN style="COLOR: blue"&gt;public static object &lt;/SPAN&gt;GetCustomMember(&lt;SPAN style="COLOR: blue"&gt;object &lt;/SPAN&gt;myObj, &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;name)
            {
                &lt;SPAN style="COLOR: #2b91af"&gt;XmlElement &lt;/SPAN&gt;xml = myObj &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XmlElement&lt;/SPAN&gt;;

                &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(xml != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
                {
                    &lt;SPAN style="COLOR: blue"&gt;for &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;XmlNode &lt;/SPAN&gt;n = xml.FirstChild; n != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;; n = n.NextSibling)
                    {
                        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(n &lt;SPAN style="COLOR: blue"&gt;is &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XmlElement &lt;/SPAN&gt;&amp;amp;&amp;amp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;.CompareOrdinal(n.Name, name) == 0)
                        {
                            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(n.HasChildNodes &amp;amp;&amp;amp; n.FirstChild == n.LastChild &amp;amp;&amp;amp; n.FirstChild &lt;SPAN style="COLOR: blue"&gt;is &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XmlText&lt;/SPAN&gt;)
                            {
                                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;n.InnerText;
                            }
                            &lt;SPAN style="COLOR: blue"&gt;else
                            &lt;/SPAN&gt;{
                                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;n;
                            }

                        }
                    }
                }
                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;OperationFailed&lt;/SPAN&gt;.Value;
            }
        }
    }
}
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Now from IronPython do this: 
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;import &lt;/SPAN&gt;clr
clr.AddReference(&lt;SPAN style="COLOR: maroon"&gt;"TestExtensions.dll"&lt;/SPAN&gt;)
&lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;TestExtensions &lt;SPAN style="COLOR: blue"&gt;import &lt;/SPAN&gt;ExtClass
foo = ExtClass.Load(&lt;SPAN style="COLOR: maroon"&gt;"test.xml"&lt;/SPAN&gt;)
&lt;SPAN style="COLOR: blue"&gt;print &lt;/SPAN&gt;foo.bar&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;This prints out “baz”. Sweet!&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8383517" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/DLR/">DLR</category><category domain="http://blogs.msdn.com/b/srivatsn/archive/tags/IronPython/">IronPython</category></item></channel></rss>