<?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>Sam Ng's Blog : DLR</title><link>http://blogs.msdn.com/samng/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>Dynamic in C# II: Basics</title><link>http://blogs.msdn.com/samng/archive/2008/11/02/dynamic-in-c-ii-basics.aspx</link><pubDate>Mon, 03 Nov 2008 02:53:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9029878</guid><dc:creator>samng</dc:creator><slash:comments>15</slash:comments><comments>http://blogs.msdn.com/samng/comments/9029878.aspx</comments><wfw:commentRss>http://blogs.msdn.com/samng/commentrss.aspx?PostID=9029878</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/samng/archive/2008/10/29/dynamic-in-c.aspx" target="_blank"&gt;Last time&lt;/a&gt;, we began to dive into dynamic binding in C# and what happens through the pipeline. This time, we'll take a simple scenario and pick apart the details of what happens under the covers, both during compile time and runtime.&lt;/p&gt;  &lt;p&gt;We can break down what the compiler does into three parts: type and member declarations with dynamics (ie methods that return &lt;font face="courier new"&gt;dynamic&lt;/font&gt;), binding and lookup, and emitting. We'll deal now with the binding aspects of &lt;font face="courier new"&gt;dynamic&lt;/font&gt;.&lt;/p&gt;  &lt;h5&gt;&lt;/h5&gt;  &lt;h5&gt;&lt;/h5&gt;  &lt;h5&gt;Dynamic binding&lt;/h5&gt;  &lt;p&gt;Dynamic binding itself can be broken into two scenarios. Lets consider the following example.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
{
    &lt;span style="color: blue"&gt;dynamic&lt;/span&gt; d = 10;
    &lt;span style="color: #2b91af"&gt;C&lt;/span&gt; c = &lt;span style="color: blue"&gt;new &lt;/span&gt;C();

    &lt;span style="color: green"&gt;// (1) Dynamic receivers.
    &lt;/span&gt;d.Foo(); &lt;span style="color: green"&gt;// Call.
    &lt;/span&gt;d.PropOrField = 10; &lt;span style="color: green"&gt;// Property.
    &lt;/span&gt;d[10] = 10; &lt;span style="color: green"&gt;// Indexer.

    // (2) Statically typed receivers (or static methods)
    //     with dynamic arguments.
    &lt;/span&gt;c.Foo(d); &lt;span style="color: green"&gt;// Instance method call.
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C&lt;/span&gt;.StaticMethod(d); &lt;span style="color: green"&gt;// Static method call.
    &lt;/span&gt;c.PropOrField = d; &lt;span style="color: green"&gt;// Property.
    &lt;/span&gt;c[d] = 10; &lt;span style="color: green"&gt;// Indexer.
    &lt;/span&gt;d++; &lt;span style="color: green"&gt;// Think of this as op_increment(d).
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;x = d + 10; &lt;span style="color: green"&gt;// Think of this as op_add(d, 10).
    &lt;/span&gt;&lt;span style="color: blue"&gt;int &lt;/span&gt;x = d; &lt;span style="color: green"&gt;// Think of this as op_implicit(d).
    &lt;/span&gt;&lt;span style="color: blue"&gt;int &lt;/span&gt;y = (&lt;span style="color: blue"&gt;int&lt;/span&gt;)d; &lt;span style="color: green"&gt;// Think of this as op_explicit(d).
&lt;/span&gt;}&lt;/pre&gt;

&lt;p&gt;Consider the first set of examples under (1). Each of these dynamic invocations happen off of the dynamically typed expression. It is clear where the dynamicity (yes, I like that word, even though it isn't one...) comes from, and where it goes. &lt;/p&gt;

&lt;p&gt;The second set of examples under (2) are a little more complex. The use of dynamic is indirect in each of these. Because the argument to each operation is dynamic, they flow into the containing operation and make them dynamic as well. As such, the compiler does sort of a mix of dynamic binding and static binding - it will use the static type of the receiver to determine the set of members to overload on, but will use the runtime types of the arguments to perform overload resolution.&lt;/p&gt;

&lt;p&gt;The first set of examples are much more straight forward to understand, so we'll use this set as our foundation for exploring the feature.&lt;/p&gt;

&lt;h5&gt;Dynamic receivers&lt;/h5&gt;

&lt;p&gt;When the compiler encounters an expression typed &lt;font face="courier new"&gt;dynamic&lt;/font&gt;, it knows to treat the subsequent operation as a dynamic operation. Whether its an index get, index set, method call etc, the result type of the operation will be determined at runtime, and so at compile time, the result of the operation must also be &lt;font face="courier new"&gt;dynamic&lt;/font&gt;. &lt;/p&gt;

&lt;p&gt;The compiler transforms all dynamic operations into what we'll call a dynamic call site. This consists of creating a compiler generated static field on a generated static class that stores the DLR site instance for the invocation, and initializing it as necessary. &lt;/p&gt;

&lt;p&gt;The DLR call site is a generic object that is generic on the delegate type of the call. More on how this delegate gets generated later. The type names may not be final yet, but currently the creation of the DLR call site takes a CallSiteBinder which is an object that knows how to perform the specific binding that is required for the call site. The DLR provides a set of standard actions that can be used to take advantage of the DLR's support for interop with dynamic objects (more on that in a later post). &lt;/p&gt;

&lt;p&gt;The call site contains a field of type T that is an instance of the delegate type that the site is instantiated with. This delegate is used to contain the DLR caching mechanism which you can learn about on &lt;a href="http://blogs.msdn.com/hugunin" target="_blank"&gt;Jim Hugunin's blog&lt;/a&gt;. It stores the results of each bind and is used to invoke the resulting operation.&lt;/p&gt;

&lt;p&gt;Once the call site has been created, the compiler then emits the code to invoke the delegate, passing it the arguments that the user passed to the call site.&lt;/p&gt;

&lt;h5&gt;&lt;/h5&gt;

&lt;h5&gt;What happens at runtime?&lt;/h5&gt;

&lt;p&gt;Once the compiler has created the DLR call site, it then invokes the delegate, which causes the DLR to do its magic with interop types, and its magic with caching. Assuming that we don't have a true IDynamicObject and we don't have a cache hit, the CallSiteBinder that we seeded the DLR site with will be invoked. C# has its own derived CallSiteBinders that will know how to perform the correct binding, and will return an expression tree which will be merged into the DLR call site's target delegate for caching. &lt;/p&gt;

&lt;p&gt;The current caching mechanism simply checks exact type matches on the arguments. For example, suppose our call looks like the following:&lt;/p&gt;

&lt;pre class="code"&gt;arg0.M(arg1, arg2, ...);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And suppose our call has arg0.Type == C, and all the arguments passed to the call are of type int. The cache check would look like the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(arg0.GetType() == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(C) &amp;amp;&amp;amp;
    arg1.GetType() == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;int&lt;/span&gt;) &amp;amp;&amp;amp;
    arg2.GetType() == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;int&lt;/span&gt;) &amp;amp;&amp;amp; 
    ...
    )
{
    Merge CallSiteBinder's bind result here.
}
... &lt;span style="color: green"&gt;// More cache checks
&lt;/span&gt;&lt;span style="color: blue"&gt;else
&lt;/span&gt;{
    Call the CallSiteBinder to bind, and update cache.
}&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;h5&gt;C# CallSiteBinder creation &lt;/h5&gt;

&lt;p&gt;The last thing we need to paint a full picture of dynamic binding is to understand what the C# CallSiteBinder implementation does. &lt;/p&gt;

&lt;p&gt;In our example, we have 3 different types of dynamic operations. We have a call, a property access, and an indexer. Each of these operations have their own unique pieces to them, but still share much of the common functionality. As such, they all are initialized with a common C# runtime binder, and are used by the runtime binder as data objects that describe the action that needs to be bound. We'll call these objects the C# payloads.&lt;/p&gt;

&lt;p&gt;A good way to think of the C# runtime binder is a mini compiler - it has many of the concepts you'd expect in a traditional compiler, such as a symbol table, and a type system, and much of the functionality as well, such as overload resolution and type substitution. &lt;/p&gt;

&lt;p&gt;Lets use the simple example of d.Foo(1) for our consideration.&lt;/p&gt;

&lt;p&gt;Once the runtime binder gets invoked, it is given the payload for the current call site, and the runtime arguments the site is being bound against. It takes the types of the all the arguments (including the receiver) and populates its symbol table with those types. It then unpacks the payload to find out the name of the operation it's trying to perform on the receiver (in this case, &amp;quot;Foo&amp;quot;), and uses reflection to load all members named &amp;quot;Foo&amp;quot; off of the runtime type of d, putting those members into its symbol table.&lt;/p&gt;

&lt;p&gt;From there, we have enough information in the binder's internal system to do the binding that the action describes. At this point, we fork off and bind based on the payload's description. &lt;/p&gt;

&lt;p&gt;One of the design choices we made was that &lt;strong&gt;the runtime binder should have the exact same semantics that the static compiler has&lt;/strong&gt;. This includes reporting the same set of errors that the compiler would produce, and perform the same set of conversions (user-defined or otherwise). &lt;/p&gt;

&lt;p&gt;As such, each payload is bound exactly as the static compiler would have. The result of the bind is an expression tree that represents the action to take if the binding was successful. Otherwise, a runtime binder exception is thrown. The resulting expression tree is then taken and merged into the call site's delegate to become part of the DLR cache mechanism, and is then invoked so that the result of the user's dynamic bind gets executed.&lt;/p&gt;

&lt;h5&gt;A slight limitation&lt;/h5&gt;

&lt;p&gt;As I mentioned, we tried to keep the philosophy of matching exactly what the static compiler would do. However, there are several scenarios that will not work in Visual Studio 2010 that we will hopefully get to in a future release.&lt;/p&gt;

&lt;p&gt;Several to note are lambdas, extension methods, and method groups. &lt;/p&gt;

&lt;p&gt;Because we currently do not have a way of representing the source of a lambda at runtime without a binding, dynamic invocations that contain lambdas produce a compile time error. &lt;/p&gt;

&lt;p&gt;Also, because we don't currently have a way of passing in using clauses and scopes during runtime, extension method lookup will also not be available for this next release. &lt;/p&gt;

&lt;p&gt;There is currently no way to represent a method group at runtime (ie there is no MethodGroup type), and so without introducing that concept into .NET, there is no good way for us to allow method groups to be represented dynamically. This means that you cannot do the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;delegate void &lt;span style="color: #2b91af"&gt;D&lt;/span&gt;&lt;/span&gt;();
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
    {
        &lt;span style="color: blue"&gt;dynamic&lt;/span&gt; d = 10;
        &lt;span style="color: #2b91af"&gt;D&lt;/span&gt; del = d.Foo; &lt;span style="color: green"&gt;// This would bind to a method group at runtime. &lt;/span&gt;
    }
}&lt;/pre&gt;

&lt;p&gt;Because we cannot represent method groups at runtime, a runtime exception will be thrown if the runtime binder binds d.Foo to a method group. &lt;/p&gt;

&lt;p&gt;Hopefully you have a better understanding about the details of what happens when C# performs a dynamic bind. Next time we'll take a look at the second set of scenarios we discussed today. We'll also introduce the Phantom Method, and describe what it does and see how it affects overload resolution. &lt;/p&gt;

&lt;p&gt;Until then, happy coding!&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2008%2f11%2f02%2fdynamic-in-c-ii-basics.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2008%2f11%2f02%2fdynamic-in-c-ii-basics.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9029878" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/samng/archive/tags/Overload+Resolution/default.aspx">Overload Resolution</category><category domain="http://blogs.msdn.com/samng/archive/tags/Dynamic/default.aspx">Dynamic</category><category domain="http://blogs.msdn.com/samng/archive/tags/DLR/default.aspx">DLR</category><category domain="http://blogs.msdn.com/samng/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category><category domain="http://blogs.msdn.com/samng/archive/tags/Runtime+binding/default.aspx">Runtime binding</category></item><item><title>Dynamic in C#</title><link>http://blogs.msdn.com/samng/archive/2008/10/29/dynamic-in-c.aspx</link><pubDate>Thu, 30 Oct 2008 00:59:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9023092</guid><dc:creator>samng</dc:creator><slash:comments>22</slash:comments><comments>http://blogs.msdn.com/samng/comments/9023092.aspx</comments><wfw:commentRss>http://blogs.msdn.com/samng/commentrss.aspx?PostID=9023092</wfw:commentRss><description>&lt;p&gt;The other day I was playing around with some office code, and I found myself writing a lot of code much like the following sample that Anders used at his &lt;a href="http://channel9.msdn.com/pdc2008/TL16/" target="_blank"&gt;PDC talk&lt;/a&gt;:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;xl = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Excel&lt;/span&gt;.Application();

&lt;span style="color: green"&gt;    &lt;/span&gt;((&lt;span style="color: #2b91af"&gt;Excel&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;Range&lt;/span&gt;)xl.Cells[1, 1]).Value2 = &lt;span style="color: #a31515"&gt;&amp;quot;Process Name&amp;quot;&lt;/span&gt;;
    ((&lt;span style="color: #2b91af"&gt;Excel&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;Range&lt;/span&gt;)xl.Cells[1, 2]).Value2 = &lt;span style="color: #a31515"&gt;&amp;quot;Memory Usage&amp;quot;&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can imagine, it very quickly became tiresome assigning the results of each call to a local variable, debugging and finding out what type it returns for my scenarios, making the cast to the strong type so that I can call certain methods on it, rinse, and repeat. &lt;/p&gt;

&lt;p&gt;This pattern is common in dynamic APIs, and cause a lot of excess code to be written that essentially is used just to make the type system happy. Wouldn't it be nice if instead, we could write something like this?&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;xl = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Excel&lt;/span&gt;.Application();

    xl.Cells[1, 1].Value2 = &lt;span style="color: #a31515"&gt;&amp;quot;Process Name&amp;quot;&lt;/span&gt;;
    xl.Cells[1, 2].Value2 = &lt;span style="color: #a31515"&gt;&amp;quot;Memory Usage&amp;quot;&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Well, in C# 4.0, we now allow you to write exactly that. One of the main features that we're working on in C# 4.0 is the dynamic late binding feature. This feature allows you to tell the compiler that the thing that I'm returning really ought to be treated like a dynamic type, and that any dispatch on it should be done dynamically. The runtime will then do the binding for you based on the runtime type of the object instead of the static compile time return type, and if the binding succeeds, then the code will succeed. This gives us exactly what we want. &lt;/p&gt;

&lt;p&gt;So how does this feature work?&lt;/p&gt;

&lt;p&gt;Firstly, we've introduced a &lt;font face="courier new"&gt;dynamic&lt;/font&gt; type into the type system. This type indicates to the compiler that all operations based on the type should be bound dynamically and not at compile time. Secondly, we've created a C# runtime binder which does the late binding for you. Lastly, we've baked in the usage of the &lt;a href="http://www.codeplex.com/dlr" target="_blank"&gt;DLR&lt;/a&gt; and are making full use of their caching and dynamic dispatch capabilities, so that you can interop with dynamic objects (objects created from Iron Python for instance). &lt;/p&gt;

&lt;h5&gt;The dynamic type&lt;/h5&gt;

&lt;p&gt;In order to start using the dynamic binding, we've got to have some way to signify to the compiler that we want our object or expression to be bound dynamically. Enter the &lt;font face="courier new"&gt;dynamic&lt;/font&gt; type. &lt;/p&gt;

&lt;p&gt;The &lt;font face="courier new"&gt;dynamic&lt;/font&gt; type is just a regular type that you can use in your code to denote local variables, fields, method return values etc. It tells the compiler that everything to do with that object or expression should be done dynamically. Consider the following example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
{
    dynamic d = SomeInitializingStatement;

    d.Foo(1, 2, 3); &lt;span style="color: green"&gt;// (1)
    &lt;/span&gt;d.Prop = 10; &lt;span style="color: green"&gt;// (2)
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;x = d + 10; &lt;span style="color: green"&gt;// (3)
    &lt;/span&gt;&lt;span style="color: blue"&gt;int &lt;/span&gt;y = d; &lt;span style="color: green"&gt;// (4)
    &lt;/span&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;y = (&lt;span style="color: blue"&gt;string&lt;/span&gt;)d; &lt;span style="color: green"&gt;// (5)
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(d); &lt;span style="color: green"&gt;// (6)
&lt;/span&gt;}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;In this example, each of the statements has some element of the dynamic type flowing through it, and is therefore dispatched dynamically. Lets consider each of them.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Since the receiver in this example is typed &lt;font face="courier new"&gt;dynamic&lt;/font&gt;, the compiler will indicate to the runtime that it needs to bind some method named &amp;quot;Foo&amp;quot; on whatever the runtime type of d happens to be, with the arguments {1, 2, 3} applied to it. &lt;/li&gt;

  &lt;li&gt;This example also has a dynamic receiver, and so the compiler indicates to the runtime that it needs to bind a property-looking-thing (could be a field or property) named &amp;quot;Prop&amp;quot;, and that it wants to set the value to 10. &lt;/li&gt;

  &lt;li&gt;In this example, the operator &amp;quot;+&amp;quot; becomes a dynamically bound operation because one of its arguments is dynamically typed. The runtime then does the normal operator overload resolution rules for &amp;quot;+&amp;quot;, finding any user-defined operators named &amp;quot;+&amp;quot; on the runtime type of d, and considering that along with the regular predefined binary operators for int. &lt;/li&gt;

  &lt;li&gt;In this example, we have an implicit conversion from the runtime type of d to int. The compiler signifies to the runtime that it should consider all implicit conversions on int and on the runtime type of d, and determine if there is a conversion to int. &lt;/li&gt;

  &lt;li&gt;This example highlights an explicit conversion to string. The compiler encodes this cast and tells the runtime to consider explicit casts to string. &lt;/li&gt;

  &lt;li&gt;In this example, despite the fact that we're calling a statically known method at compile time, we have dynamic arguments. As such, we cannot perform overload resolution correctly at compile time, and so the dynamic-ness of d flows out to its containing call, and we end up dispatching Console.WriteLine dynamically as well. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are several other scenarios that dynamic flows out to, but I've listed these to give you a general idea of what the dynamic type's implications are.&lt;/p&gt;

&lt;p&gt;Now, we should note that the &lt;font face="courier new"&gt;dynamic&lt;/font&gt; type is really just syntactic sugar to signify to the compiler that it should treat bindings dynamically. In metadata, &lt;font face="courier new"&gt;dynamic&lt;/font&gt; is just &lt;font face="courier new"&gt;object&lt;/font&gt; with an attribute signifying its dynamicity (if that's even a word... I don't think it is though!). &lt;/p&gt;

&lt;h5&gt;What happens at compile time?&lt;/h5&gt;

&lt;p&gt;For each dynamic operation, the compiler generates calls into the DLR, and takes advantage of its call sites. For more information about the DLR and what call sites are, my colleague Jim Hugunin gave an excellent talk at PDC about it - you can view his blog &lt;a href="http://blogs.msdn.com/hugunin/" target="_blank"&gt;here&lt;/a&gt;, and watch his talk &lt;a href="http://channel9.msdn.com/pdc2008/TL10/" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The DLR call site takes a set of standard actions which indicate what type of dynamic action we want to take. The C# compiler emits a subclass of these standard actions, annotated with some C# specific details, and emits invocations of the call sites in place of the call that the user makes. For instance, this code sample gets translated into something like the following pseudocode:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: green"&gt;// This code...
&lt;/span&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
{
    &lt;span style="color: blue"&gt;dynamic &lt;/span&gt;d = SomeInitializingStatement;
    d.Foo(1, 2, d);
}

&lt;span style="color: green"&gt;// transforms into this code.
&lt;/span&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
{
    &lt;span style="color: blue"&gt;dynamic &lt;/span&gt;d = SomeInitializingStatement;
    _csharpCallAction = &lt;span style="color: blue"&gt;new &lt;/span&gt;CSharpCallAction(&lt;span style="color: #a31515"&gt;&amp;quot;Foo&amp;quot;&lt;/span&gt;);
    _dlrSite&amp;lt;T&amp;gt; = &lt;span style="color: blue"&gt;new &lt;/span&gt;Site&amp;lt;T&amp;gt;(_csharpCallAction); &lt;span style="color: green"&gt;// Create the site. &lt;/span&gt;
    _dlrSite.Target(1, 2, d); &lt;span style="color: green"&gt;// Invoke the delegate. &lt;/span&gt;
}&lt;/pre&gt;

&lt;p&gt;If you want more information on this, my colleague Chris Burrows has written an &lt;a href="http://blogs.msdn.com/cburrows/archive/2008/10/27/c-dynamic.aspx" target="_blank"&gt;excellent blog&lt;/a&gt; on the dynamic type and what the compiler generates. &lt;/p&gt;

&lt;p&gt;Note that the site creation pseudocode specifies a generic argument, T. This argument is a delegate type that represents the signature of the call. So in our example, our call takes 2 integer arguments and a dynamic argument, and has a dynamic receiver. T would then be a delegate that represents that.&lt;/p&gt;

&lt;p&gt;Invoking that delegate invokes the C# runtime binder, which binds the expression based on the runtime types of the arguments and the receiver. &lt;/p&gt;

&lt;h5&gt;What happens at runtime?&lt;/h5&gt;

&lt;p&gt;When the DLR delegate gets invoked, it does a couple of cool things that I'll describe briefly. For more information, check out &lt;a href="http://blogs.msdn.com/hugunin/" target="_blank"&gt;Jim Hugunin's blog&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The DLR checks a cache to see if the given action has already been bound against the current set of arguments. So in our example, we would do a type match based on 1, 2, and the runtime type of d. If we have a cache hit, then we return the cached result. &lt;/li&gt;

  &lt;li&gt;If we do not have a cache hit, then the DLR checks to see if the receiver is an IDynamicObject. These guys are essentially objects which know how to take care of their own binding, such as COM IDispatch objects, real dynamic objects such as Ruby or Python ones, or some .NET object that implements the IDynamicObject interface. If it is any of these, then the DLR calls off to the IDO and asks it to bind the action. 
    &lt;br /&gt;

    &lt;br /&gt;Note that the result of invoking the IDO to bind is an expression tree that represents the result of the binding. &lt;/li&gt;

  &lt;li&gt;If it is not an IDO, then the DLR calls into the language binder (in our case, the C# runtime binder) to bind the operation. The C# runtime binder will bind the action, and will return an expression tree representing the result of the bind. &lt;/li&gt;

  &lt;li&gt;Once step 2 or 3 have happened, the resulting expression tree is merged into the caching mechanism so that any subsequent calls can run against the cache instead of being rebound. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Jim gives a great description of points 1, 2, and 4, which deal with the DLR specifics, so I'm going to elaborate on what happens in step 3.&lt;/p&gt;

&lt;h5&gt;The C# runtime binder&lt;/h5&gt;

&lt;p&gt;The C# runtime binder uses Reflection to populate its internal symbol table to determine what to bind to. Each of the C# specific actions encodes the type of the binding, along with extra information that allows us to determine how to bind the action.&lt;/p&gt;

&lt;p&gt;For example, if the argument is known at compile time to have a static type, then that type will be marked in the C# action, and will be used as the type of the argument during runtime binding. If it is known at compile time to be typed &lt;font face="courier new"&gt;dynamic&lt;/font&gt; (ie it is a variable of type &lt;font face="courier new"&gt;dynamic&lt;/font&gt;, or is an expression that returns &lt;font face="courier new"&gt;dynamic&lt;/font&gt;), then the runtime binder will use reflection to determine its runtime type and use that type as the type of the argument.&lt;/p&gt;

&lt;p&gt;The runtime binder populates its symbol table as needed. For instance, in our example, we were calling the method Foo. The runtime binder will load all members named Foo on the type of the receiver into the symbol table.&lt;/p&gt;

&lt;p&gt;It then populates the necessary conversions for each of the argument types. Since we may need to coerce the arguments to types that match the method calls (using user-defined conversions as necessary), the binder loads those conversions into the symbol table as well.&lt;/p&gt;

&lt;p&gt;It then performs overload resolution exactly like the static compiler does. That means that we get the exact same semantics as the static compiler. It also means that we get the same error semantics and messages - a failed binding at runtime results in an exception being thrown, which encapsulates the error message that you would have gotten at compile time.&lt;/p&gt;

&lt;p&gt;It then takes the result of overload resolution and generates an expression tree that represents the result, and returns that back to the DLR. &lt;/p&gt;

&lt;h5&gt;A summary&lt;/h5&gt;

&lt;p&gt;So that's a brief summary of what the dynamic pipeline looks like. Of course, I've glossed over a lot of the details, but I'll be covering those details in my future posts. Until next time, some questions to ponder:&lt;/p&gt;

&lt;p&gt;What happens when the receiver is known statically but the arguments are dynamic? What happens if the methods we're trying to bind against are private? What about operators - how does resolution work on them? &lt;/p&gt;

&lt;p&gt;These, and more, I'll aim to address in my subsequent posts. &lt;/p&gt;

&lt;p&gt;As always, happy coding!&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2008%2f10%2f29%2fdynamic-in-c.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2008%2f10%2f29%2fdynamic-in-c.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9023092" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/samng/archive/tags/Overload+Resolution/default.aspx">Overload Resolution</category><category domain="http://blogs.msdn.com/samng/archive/tags/Dynamic/default.aspx">Dynamic</category><category domain="http://blogs.msdn.com/samng/archive/tags/DLR/default.aspx">DLR</category><category domain="http://blogs.msdn.com/samng/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category><category domain="http://blogs.msdn.com/samng/archive/tags/Runtime+binding/default.aspx">Runtime binding</category></item></channel></rss>