Dynamic in C# – The DLR (Part 2)

Posted: September 13, 2009 in .NET, C#
Tags: , ,

In my last post I have covered the basics of dynamic languages and now we will see how this dynamic languages fit into the CLR.The core purpose of CLR was to host code written in multiple programming languages.But traditionally it had support for the static languages only.Dynamic Language Runtime (DLR) was conceived as a set of additional libraries in order to fit the dynamic languages into the CLR.Apart from allowing the new dynamic languages to come on board, DLR also facilitates addition of dynamic capabilities in existing CLR supported languages (static) like C#.

The high level architecture of DLR is shown below:


Here we will discuss about the following three key components of the DLR and how they cooperate to execute dynamic languages:

  • Expression Trees – This is mechanism to store program/code in form of data.This is stored in a tree based data structure where each node forms an expression.This was introduced in 3.5 to support LINQ and now this is extended in .NET 4.0 to support the dynamic languges.The code below shows an expression tree for x + 5

Expression> expTree = x => x + 5;

ParameterExpression param = (ParameterExpression)expTree.Parameters[0];

BinaryExpression operation = (BinaryExpression)expTree.Body;

ParameterExpression left = (ParameterExpression)operation.Left;

ConstantExpression right = (ConstantExpression)operation.Right;

Console.WriteLine(“Expression: {0} => {1} {2} {3}”,

param.Name, left.Name, operation.NodeType, right.Value);

The output will be : Expression: x => x Add 5

  • Call Sites – They provide a caching mechanism for every dynamic call and stores the rules based on which a dynamic operation needs to be performed thus providing better performance.
  • Binders – Binders provide language specific support by to the call site on how to perform a dynamic operation.Whenever there is a cache miss in the call site binders provide the necessary information to execute the call to the call site in form of expression trees.

Now let’s see how these three components evaluates a simple expression “a+b” in a dynamic language

  1. Compiler will emit code to create dynamic Callsite.
  2. At runtime control will be passed to the Callsite to evaluate the expression.First time there no information about this expression in the Callsite cache.There will be a cache miss and control will be passed to the Binder.
  3. The Binder will look for the type of “a” and “b” and will return an expression like (int) a + (int) b if “a” and “b” are both int.
  4. CallSite can directly evaluate this expression but in order to cache this data it needs the information in a format which says if “a” and “b” are int then implementation will be “(int) a + (int) b“.This condition check is called Test and test along with implementation is called the Rule.This Rule is returned by the binder to be cached by the Callsite

This is very high level overview of DLR.In the next post we will discuss about the dynamic keyword of C# and then get into the details of internal implementation using DLR.

  1. […] at runtime and the appropriate method will be executed.As I have explained in the previous post the following steps will be performed at […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.