<?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 : DLR</title><link>http://blogs.msdn.com/cburrows/archive/tags/DLR/default.aspx</link><description>Tags: DLR</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>MSDN Channel 9 Video on Dynamic</title><link>http://blogs.msdn.com/cburrows/archive/2009/09/22/msdn-channel-9-video-on-dynamic.aspx</link><pubDate>Wed, 23 Sep 2009 00:44:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9898200</guid><dc:creator>cburrows</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cburrows/comments/9898200.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cburrows/commentrss.aspx?PostID=9898200</wfw:commentRss><wfw:comment>http://blogs.msdn.com/cburrows/rsscomments.aspx?PostID=9898200</wfw:comment><description>&lt;p&gt;About a month ago, &lt;a href="http://blogs.msdn.com/samng/"&gt;Sam Ng&lt;/a&gt; and I sat down with &lt;a href="http://blogs.msdn.com/charlie/"&gt;Charlie Calvert&lt;/a&gt; and his video camera to record an interview-style video about dynamic. Have a look!&lt;/p&gt;  &lt;p&gt;&lt;a title="http://channel9.msdn.com/posts/CharlieCalvert/CSharp-4-Dynamic-with-Chris-Burrows-and-Sam-Ng/" href="http://channel9.msdn.com/posts/CharlieCalvert/CSharp-4-Dynamic-with-Chris-Burrows-and-Sam-Ng/"&gt;http://channel9.msdn.com/posts/CharlieCalvert/CSharp-4-Dynamic-with-Chris-Burrows-and-Sam-Ng/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;If you view it, I hope you find it helpful and informative. Also, I clearly need to take some kind of instruction on what to do with oneself on video when the other person is talking. That pen was &lt;em&gt;very&lt;/em&gt; interesting.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9898200" 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/DLR/default.aspx">DLR</category></item><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," Part II</title><link>http://blogs.msdn.com/cburrows/archive/2008/10/28/c-dynamic-part-ii.aspx</link><pubDate>Tue, 28 Oct 2008 19:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9020443</guid><dc:creator>cburrows</dc:creator><slash:comments>21</slash:comments><comments>http://blogs.msdn.com/cburrows/comments/9020443.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cburrows/commentrss.aspx?PostID=9020443</wfw:commentRss><wfw:comment>http://blogs.msdn.com/cburrows/rsscomments.aspx?PostID=9020443</wfw:comment><description>&lt;p&gt;Yesterday, I made an attempt to introduce the C# dynamic feature by describing what change there has been to the language, what scenarios we hope to affect by that change, and some overview of what dynamic operations look like in C# 4 as it stands in the preview distributed at PDC 2008.&lt;/p&gt;  &lt;p&gt;One thing that I did not mention was this: that the language changes are entirely focused around consumption of dynamic types, not definition of those types. This is not to say that you cannot define dynamic types--just that the C# language has not change in any way so as to provide you a shortcut. We have not added a &amp;quot;method_missing,&amp;quot; nor are any of the regular types you define in C# capable of somehow dynamically acquiring properties. And you can't say something like &amp;quot;dynamic class C&amp;quot; to define such a class.&lt;/p&gt;  &lt;p&gt;If you want to define a new type that accepts dynamic operations, then it's easy to do so, and you don't even need C# 4. You just implement IDynamicObject, which is the interface that tells the DLR, &amp;quot;I know how to dispatch operations on myself.&amp;quot; It's a simple interface, in the spirit of IQueryable, with a single method that returns a &amp;quot;MetaObject&amp;quot; to do the real heavy lifting. &lt;/p&gt;  &lt;p&gt;I want to make clear at this point that I am about to provide an example of how to use the DLR, but that I am only doing so in order to demonstrate how these objects play with the C# 4 language features. There are other blogs that are certainly better sources for information about the DLR. &lt;/p&gt;  &lt;p&gt;So, here's an implementation:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;font color="#0000ff"&gt;public class&lt;/font&gt; &lt;font color="#2b91af"&gt;MyDynamicObject&lt;/font&gt; : &lt;font color="#2b91af"&gt;IDynamicObject&lt;/font&gt;
{
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#2b91af"&gt;MetaObject&lt;/font&gt; GetMetaObject(&lt;font color="#2b91af"&gt;Expression&lt;/font&gt; parameter)
  {
    &lt;font color="#0000ff"&gt;return new&lt;/font&gt; &lt;font color="#2b91af"&gt;MyMetaObject&lt;/font&gt;(parameter);
  }
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Simple enough! Here's the MyMetaObject definition. The MetaObject &amp;quot;knows how&amp;quot; to respond to a variety of actions including method calls, property sets/gets, etc. I'll just handle those (no best practices here; this is a minimal implementation):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;font color="#0000ff"&gt;public class&lt;/font&gt; &lt;font color="#2b91af"&gt;MyMetaObject&lt;/font&gt; : &lt;font color="#2b91af"&gt;MetaObject&lt;/font&gt;
{
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#2b91af"&gt;MyMetaObject&lt;/font&gt;(&lt;font color="#2b91af"&gt;Expression&lt;/font&gt; parameter)
    : &lt;font color="#0000ff"&gt;base&lt;/font&gt;(parameter, &lt;font color="#2b91af"&gt;Restrictions&lt;/font&gt;.Empty)
  {
  }
  
  &lt;font color="#0000ff"&gt;public override&lt;/font&gt; &lt;font color="#2b91af"&gt;MetaObject&lt;/font&gt; Call(&lt;font color="#2b91af"&gt;CallAction&lt;/font&gt; action, &lt;font color="#2b91af"&gt;MetaObject&lt;/font&gt;[] args)
  {
    &lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(&lt;font color="#a31515"&gt;&amp;quot;Call of method {0}&amp;quot;&lt;/font&gt;, action.Name);
    &lt;font color="#0000ff"&gt;return this&lt;/font&gt;;
  }

  &lt;font color="#0000ff"&gt;public override&lt;/font&gt; &lt;font color="#2b91af"&gt;MetaObject&lt;/font&gt; SetMember(&lt;font color="#2b91af"&gt;SetMemberAction&lt;/font&gt; action, &lt;font color="#2b91af"&gt;MetaObject&lt;/font&gt;[] args)
  {
    &lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(&lt;font color="#a31515"&gt;&amp;quot;SetMember of property {0}&amp;quot;&lt;/font&gt;, action.Name);
    &lt;font color="#0000ff"&gt;return this&lt;/font&gt;;
  }

  &lt;font color="#0000ff"&gt;public override&lt;/font&gt; &lt;font color="#2b91af"&gt;MetaObject&lt;/font&gt; GetMember(&lt;font color="#2b91af"&gt;GetMemberAction&lt;/font&gt; action, &lt;font color="#2b91af"&gt;MetaObject&lt;/font&gt;[] args)
  {
    &lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(&lt;font color="#a31515"&gt;&amp;quot;GetMember of property {0}&amp;quot;&lt;/font&gt;, action.Name);
    &lt;font color="#0000ff"&gt;return this&lt;/font&gt;;
  }
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is exactly what, say, IronPython might do to implement the dictionary lookup that it needs to do for its members. And when I say &amp;quot;exactly,&amp;quot; I mean &amp;quot;sort of&amp;quot; because of course IronPython's implementation is considerably more complex and robust. But this gets the job done. Suppose I want to now use C# to invoke these things using the new syntax. That's the easy part! Check this out:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;font color="#0000ff"&gt;public class&lt;/font&gt; &lt;font color="#2b91af"&gt;Program&lt;/font&gt;
{
  &lt;font color="#0000ff"&gt;static void&lt;/font&gt; Main(&lt;font color="#0000ff"&gt;string&lt;/font&gt;[] args)
  {
    &lt;font color="#0000ff"&gt;dynamic&lt;/font&gt; d = &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;MyDynamicObject&lt;/font&gt;();

    d.P3 = d.M1(d.P1, d.M2(d.P2));
  }
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I take my MyDynamicObject that I defined above, and then I get a few properties, call a few methods, and set a property for good measure. If you compile this, you get the following output:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;GetMember of property P1
GetMember of property P2
Call of method M2
Call of method M1
SetMember of property P3&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;I think that's pretty cool.&lt;/p&gt;

&lt;p&gt;Next time I'll talk more about the dynamic type again, in the C# language, and how it behaves. I just wanted to take a little diversion to clear up the fact that we've really done a lot of cool work on the consumption side, and your code ought to look better for it when you're consuming these things. If you're defining them, then you're in DLR-land, and you might even be writing in python or ruby or some other language. I'll provide pointers to those issues as I get them. &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" mce_href="http://blogs.msdn.com/cburrows/archive/2008/10/27/c-dynamic.aspx"&gt;C# &amp;quot;dynamic&amp;quot;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9020443" 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/DLR/default.aspx">DLR</category></item></channel></rss>