<?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>Chris Burrows' Blog : PDC</title><link>http://blogs.msdn.com/cburrows/archive/tags/PDC/default.aspx</link><description>Tags: PDC</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>C# "dynamic," Part III</title><link>http://blogs.msdn.com/cburrows/archive/2008/10/29/c-dynamic-part-iii.aspx</link><pubDate>Wed, 29 Oct 2008 06:59:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9022088</guid><dc:creator>cburrows</dc:creator><slash:comments>22</slash:comments><comments>http://blogs.msdn.com/cburrows/comments/9022088.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cburrows/commentrss.aspx?PostID=9022088</wfw:commentRss><wfw:comment>http://blogs.msdn.com/cburrows/rsscomments.aspx?PostID=9022088</wfw:comment><description>&lt;p&gt;Here are a few good resources that you ought to look at for information about dynamic from PDC:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://channel9.msdn.com/pdc2008/TL16/"&gt;&lt;strong&gt;Anders Hejlsberg's talk, &amp;quot;The Future of C#&amp;quot;&lt;/strong&gt;&lt;/a&gt; - Among other things, this talk is a great introduction to dynamic. I recommend it highly! There are some good demos, and Anders' presentation is very compelling. I especially like the parts where people applaud. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://channel9.msdn.com/pdc2008/TL10/"&gt;&lt;strong&gt;Jim Hugunin's talk, &amp;quot;Deep Dive: Dynamic Languages in Microsoft .NET&amp;quot;&lt;/strong&gt;&lt;/a&gt; - Jim is the DLR's architect. There are some details about implementing your own dynamic objects, which I touched on only superficially yesterday. He also talks about the way that C# (or any language) generates dynamic call sites and what they do at runtime.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://code.msdn.microsoft.com/csharpfuture/Release/ProjectReleases.aspx?ReleaseId=1686"&gt;&lt;strong&gt;Mads Torgerson's white paper, &amp;quot;New features in C# 4.0&amp;quot;&lt;/strong&gt;&lt;/a&gt; - Some more information, this time written by the language PM. Mads is the guy who'll ultimately write and edit the updated language spec, which doesn't yet exist.&lt;/p&gt;  &lt;p&gt;If you watch these presentations, and you start to play around with the PDC 2008 CTP, you may notice something strange. A few things just won't work. For instance, when you try to get an indexer on a dynamic (d[0]) or perform a binary operation with a dynamic (&amp;quot;Hello from C# &amp;quot; + d), the compiler is going to tell you you can't do that. And in Jim's talk, he implements IDynamicObject with a great helper class called DynamicObject where you only need to override some virtual methods. But the PDC bits don't have that type.&lt;/p&gt;  &lt;p&gt;It may be obvious, but the thing that's going on here is that the CTP build for PDC was snapped sometime in the summer, and some of the presenters are using a newer, more complete build. We've done a lot of work since the CTP was snapped, and I regret that you can't see it all first hand right now, but I hope you'll understand that this is the nature of a CTP. I'll try to be clear when I post about what will work for you, and what won't.&lt;/p&gt;  &lt;p&gt;For instance, Part II in this series (the implementation of IDynamicObject) &lt;em&gt;will&lt;/em&gt; work for you! I don't think Part I will, though (it uses indexers).&lt;/p&gt;  &lt;p&gt;If you're wondering what &lt;em&gt;does&lt;/em&gt; work for dynamic C# in the CTP build, here's the short story: (a) dynamic &lt;strong&gt;method calls&lt;/strong&gt;, (b) dynamic &lt;strong&gt;property gets/sets&lt;/strong&gt;, and (c) dynamic &lt;strong&gt;conversion&lt;/strong&gt; sites. &lt;/p&gt;  &lt;p&gt;Previous posts in this series: &lt;a href="http://blogs.msdn.com/cburrows/archive/2008/10/27/c-dynamic.aspx"&gt;C# &amp;quot;dynamic&amp;quot;&lt;/a&gt;; &lt;a href="http://blogs.msdn.com/cburrows/archive/2008/10/28/c-dynamic-part-ii.aspx"&gt;C# &amp;quot;dynamic,&amp;quot; Part II&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9022088" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cburrows/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/cburrows/archive/tags/dynamic/default.aspx">dynamic</category><category domain="http://blogs.msdn.com/cburrows/archive/tags/PDC/default.aspx">PDC</category><category domain="http://blogs.msdn.com/cburrows/archive/tags/DLR/default.aspx">DLR</category></item><item><title>C# "dynamic"</title><link>http://blogs.msdn.com/cburrows/archive/2008/10/27/c-dynamic.aspx</link><pubDate>Mon, 27 Oct 2008 17:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9019177</guid><dc:creator>cburrows</dc:creator><slash:comments>34</slash:comments><comments>http://blogs.msdn.com/cburrows/comments/9019177.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cburrows/commentrss.aspx?PostID=9019177</wfw:commentRss><wfw:comment>http://blogs.msdn.com/cburrows/rsscomments.aspx?PostID=9019177</wfw:comment><description>&lt;P&gt;PDC 2008 arrives today, and that means that I am finally able to talk publicly about some of the things I've been working on for the next version of C#. What a relief! I am excited for the world to see some of this stuff, and to start receiving more customer feedback about it.&lt;/P&gt;
&lt;P&gt;This afternoon, Anders Hejlsberg presented a talk called "The Future of C#" that I confess I have not seen a dry-run of. I'm sure it went fabulously, though. In that talk, he introduced the "dynamic" feature in C# 4. The short story is that we plan to allow users to perform operations, the details of which are not known until runtime, using the dispatch mechanism supported by the DLR (Jim Hugunin is giving another talk today called "Deep Dive: Dynamic Languages in Microsoft .NET").&lt;/P&gt;
&lt;P&gt;Charlie Calvert and Mads Torgersen &lt;A href="http://blogs.msdn.com/charlie/archive/2008/01/25/future-focus.aspx" mce_href="http://blogs.msdn.com/charlie/archive/2008/01/25/future-focus.aspx"&gt;let slip&lt;/A&gt; a few months ago that this was coming, and at the time we got a lot of feedback about the particulars of that proposal (which you might like to go back and look at, but keep in mind that it is out-dated). In fact, we've been through a number of designs, and we've implemented a prototype and a few false starts before we settled on what we believe is the best design: dynamic is a type.&lt;/P&gt;
&lt;H3&gt;The type "dynamic"&lt;/H3&gt;
&lt;P&gt;So here's an example of what you might see in C# 4, the result of which is a runtime/late-bound dispatch to the method "Foo" on some local variable:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;dynamic&lt;/SPAN&gt; d = GetADynamicThing();
d.Foo();&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;As you can see, the local variable "d" is of type dynamic, which is a proper type supported by the compiler, and which you can mention in most places that you can mention a type. This code compiles, and it would have compiled no matter what method name you had used. In that sense, "dynamic" is a very special type. It appears to support almost any method name, any property or field, in fact, most anything that you could do with some variable, you can do with a dynamic variable.&lt;/P&gt;
&lt;P&gt;The difference of course is that instead of emitting an IL call to something called Foo, which the compiler clearly cannot do since there is no type involved that defines anything called Foo, the compiler emits a "dynamic call site" that manages that operation at runtime, using the C# Runtime binder in conjunction with the DLR, both of which are library components. More details on this later.&lt;/P&gt;
&lt;P&gt;This allows you to use the C# language, with the brevity and syntax you've become accustomed to, to invoke methods on python or ruby objects, or on any COM IDispatch object, even without an interop assembly, or on any other object that "knows how to dispatch itself" (you could imagine, say, a DOM that uses this to expose its content as properties). And you can do this on regular old .NET objects too.&lt;/P&gt;
&lt;P&gt;And since dynamic is a type, you can use it in more that just locals. For example, this compiles:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;C&lt;/SPAN&gt;
{
    &lt;SPAN style="COLOR: blue"&gt;public dynamic&lt;/SPAN&gt; myField;
    &lt;SPAN style="COLOR: blue"&gt;public dynamic&lt;/SPAN&gt; MyProp { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
    &lt;SPAN style="COLOR: blue"&gt;public dynamic&lt;/SPAN&gt; MyMethod(&lt;SPAN style="COLOR: blue"&gt;dynamic&lt;/SPAN&gt; d)
    {
        &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; d.Foo();
    }
    &lt;SPAN style="COLOR: blue"&gt;public delegate dynamic&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyDelegate&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;dynamic&lt;/SPAN&gt; d);
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you were to get the value of MyProp from some instance of C, and then do anything to it (maybe you could call the method Foo?), then that would be a dynamic dispatch.&lt;/P&gt;
&lt;H3&gt;A little behind-the-scenes&lt;/H3&gt;
&lt;P&gt;There is a lot going on here that I am omitting, the details of which I plan to bore/fascinate you with over the course of the next few weeks. Let me at least show you what one of these things looks like if we crack open the assembly. Assume the following class definition:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;C&lt;/SPAN&gt;
{
    &lt;SPAN style="COLOR: blue"&gt;public dynamic&lt;/SPAN&gt; MyMethod(&lt;SPAN style="COLOR: blue"&gt;dynamic&lt;/SPAN&gt; d)
    {
        &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; d.Foo();
    }
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's one method that includes some dynamic params as well as a single "dynamic call site" to call the method Foo. Reflector tells you that the assembly actually looks something like this (simplified a bit):&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;C&lt;/SPAN&gt;
{
    [&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Dynamic&lt;/SPAN&gt;]
    &lt;SPAN style="COLOR: blue"&gt;public object&lt;/SPAN&gt; MyMethod([&lt;SPAN style="COLOR: #2b91af"&gt;Dynamic&lt;/SPAN&gt;] &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; d)
    {
        &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;MyMethodo__SiteContainer0&lt;/SPAN&gt;.p__Site1 == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
        {
            &lt;SPAN style="COLOR: #2b91af"&gt;MyMethodo__SiteContainer0&lt;/SPAN&gt;.p__Site1 = 
              &lt;SPAN style="COLOR: #2b91af"&gt;CallSite&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;CallSite&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;&amp;gt;
              .Create(new &lt;SPAN style="COLOR: #2b91af"&gt;CSharpCallPayload&lt;/SPAN&gt;(
                &lt;SPAN style="COLOR: #2b91af"&gt;CSharpCallFlags&lt;/SPAN&gt;.None, &lt;SPAN style="COLOR: #a31515"&gt;"Foo"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;), &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;, 
                new &lt;SPAN style="COLOR: #2b91af"&gt;CSharpArgumentInfo&lt;/SPAN&gt;[] { 
                  &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CSharpArgumentInfo&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;CSharpArgumentInfoFlags&lt;/SPAN&gt;.None,
                &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) }));
        }
        return &lt;SPAN style="COLOR: #2b91af"&gt;MyMethodo__SiteContainer0&lt;/SPAN&gt;.p__Site1
          .Target(&lt;SPAN style="COLOR: #2b91af"&gt;MyMethodo__SiteContainer0&lt;/SPAN&gt;.p__Site1, d);
    }

    [&lt;SPAN style="COLOR: #2b91af"&gt;CompilerGenerated&lt;/SPAN&gt;]
    &lt;SPAN style="COLOR: blue"&gt;private static class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyMethodo__SiteContainer0&lt;/SPAN&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;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;CallSite&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;&amp;gt; p__Site1;
    }
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;As you can see, the publicly visible members that use the type dynamic actually, behind the scenes, use the type object when they are emitted. There is no framework type "dynamic." However, those "objects" are all decorated in such a way that the compiler (or anyone else) can tell that they are meant to be handled dynamically.&lt;/P&gt;
&lt;P&gt;There is also a static field to hold one of the DLR's dynamic call sites for each dynamic operation that you perform. In this case, there is just one, and it corresponds to the method call to Foo. It is initialized inline with the code that uses it in a lazy manner, and invoked with the "Target" field which is actually a delegate that does the right work.&lt;/P&gt;
&lt;P&gt;I admit that code looks scary! But the important thing is that it's something that you don't have to write. The compiler does it all for you. Again, lots of detail is being left out here--for instance, what really happens at runtime, the perf characteristics, and lots of juicy language considerations--but we'll get to all that later.&lt;/P&gt;
&lt;H3&gt;A scenario we made better by all this&lt;/H3&gt;
&lt;P&gt;I want to end this post by responding to something that Scott Hanselman posted a little while ago, about the pain of programming against COM in C#. Go look at this post:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;A title=http://www.hanselman.com/blog/BackToBasicsVarDim.aspx href="http://www.hanselman.com/blog/BackToBasicsVarDim.aspx" mce_href="http://www.hanselman.com/blog/BackToBasicsVarDim.aspx"&gt;http://www.hanselman.com/blog/BackToBasicsVarDim.aspx&lt;/A&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;...and consider the 53-line horror of what you need to write to talk to Word via its COM object model. In C# anyway. Especially the last half, where there are a few calls to "InvokeMember." Well, one of the benefits we get from the dynamic feature is that all this InvokeMember nonsense is no longer required. Check out the same 53 lines in C# 4 (now considerably smaller):&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ApplicationClass&lt;/SPAN&gt; WordApp = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ApplicationClass&lt;/SPAN&gt;();
WordApp.Visible = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;
&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fileName = Path.Combine(
  &lt;SPAN style="COLOR: #2b91af"&gt;AppDomain&lt;/SPAN&gt;.CurrentDomain.BaseDirectory, &lt;SPAN style="COLOR: #a31515"&gt;@"..\..\..\NewTest.doc"&lt;/SPAN&gt;);
&lt;SPAN style="COLOR: #2b91af"&gt;Document&lt;/SPAN&gt; aDoc = WordApp.Documents.Open(
  fileName, ReadOnly: &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;, Visible: &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;);

aDoc.Activate();

&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyValue = 
  aDoc.CustomDocumentProperties[&lt;SPAN style="COLOR: #a31515"&gt;"CustomProperty1"&lt;/SPAN&gt;].Value;
aDoc.CustomDocumentProperties[&lt;SPAN style="COLOR: #a31515"&gt;"CustomProperty1"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: #a31515"&gt;"Hanselman"&lt;/SPAN&gt;;

&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;Range&lt;/SPAN&gt; r &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; aDoc.StoryRanges)
{
    r.Fields.Update();
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you look closely, you'll notice that we don't even mention the dynamic keyword in this code snippet. It comes in anyway, though, given that we import COM interop interfaces specially so that you automatically get dynamic calls where appropriate.&lt;/P&gt;
&lt;P&gt;If you look closely again, you'll see that there's more here than just dynamic. How about that syntax on the Open() call? More on that later.&lt;/P&gt;
&lt;P&gt;Like I said above several times, there's a lot about this that I've left unsaid, and in fact there are a few bits that are even yet-undesigned. I'll follow up with more information soon. If you have feedback or questions, please let me know! And happy PDCing if you're in LA this week.&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#c0c0c0&gt;PS, I feel obliged to tell you that this post describes a product that has not shipped and is not yet complete. I cannot make any claim to know exactly what C# 4 will look like when it does ship. The code here corresponds roughly to what you can do with the PDC 2008 release of Visual Studio 2010 CTP. This posting is provided "AS IS" with no warranties, and confers no rights.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9019177" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cburrows/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/cburrows/archive/tags/dynamic/default.aspx">dynamic</category><category domain="http://blogs.msdn.com/cburrows/archive/tags/PDC/default.aspx">PDC</category></item></channel></rss>