joydip_kanjilal
Contributor

Exploring the Dynamic Language Runtime in .Net

opinion
Feb 26, 20154 mins
Software Development

Leverage the Dynamic Language Runtime to facilitate interoperability between statically and dynamically typed languages used in your application

Statically typed languages are those in which you would need to specify the type of an object at the time when you define it. Examples of statically typed languages include C#, VB, and C++. On the contrary, in dynamically typed languages, the type of an object is determined at runtime  — only at the time when a value is assigned to the type. Python, Ruby, and JavaScript are examples of dynamically typed languages.

The DLR (Dynamic Language Runtime) runs on top of the CLR (Common Language Runtime) and adds dynamism to the managed environment of .Net — you can use it to implement dynamic features in your application. In essence, the DLR enables interoperability between statically typed and dynamically typed languages inside the context of the CLR. You can use the DLR to share libraries and objects with dynamic languages. In this article I would present an overview of the Dynamic Language Runtime environment in Microsoft .Net.

You can get an open source version of the DLR from Codeplex.

What is the DLR?

The DLR is an outcome of Microsoft’s effort to have services run on top of the CLR and provide interoperability amongst statically and dynamically typed languages. Support for the Dynamic Language Runtime environment is facilitated by the System.Dynamic namespace. The MSDN states: “The dynamic language runtime (DLR) is a runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR). The DLR makes it easier to develop dynamic languages to run on the .Net Framework and to add dynamic features to statically typed languages.”

How is it helpful?

The services provided by the DLR include support for a dynamic type system, a standard hosting model as well as dynamic code generation and dispatch. At a quick glance, the benefits provided by the DLR include:

  1. Provides support for dynamic features in statically typed languages. With the DLR in place, you can create dynamically typed objects and use them together with your statically typed objects in your application.
  2. Enables seamless porting of dynamic languages to the .Net Framework. The DLR enables you to port dynamic languages into the .Net Framework easily. To leverage the DLR features, all your dynamic language need to do have is the ability to produce expression trees and runtime helper routines.
  3. Facilitates sharing of libraries and objects. The DLR enables you to create objects and libraries in one language to be accessed from another language.
  4. Provides support for dynamic method dispatch and invocation. The DLR provides support for dynamic method invocation and dispatch using advanced polymorphic caching.

The Dynamic Language Runtime Subsystem

The DLR subsystem is basically comprised of the three layers. These include the following:

  1. Expression trees — the DLR makes use of expression trees to represent language semantics.
  2. Call site caching — method calls using dynamic objects are cached in the memory so that the DLR can use the cache history for subsequent calls to the same method for faster dispatch.
  3. Dynamic object interoperability — the DLR enables interoperability between statically and dynamically typed languages. The DLR includes a collection of types — classes and interfaces in the System.Dynamic namespace. You can leverage the IDynamicMetaObjectProvider interface and the DynamicMetaObject, DynamicObject, and ExpandoObject classes to create dynamic frameworks.

Language Binders

The language binders in the DLR help it to talk to other languages. So, for each dynamic language you would typically have a binder that can interact with it. As an example the following are the commonly used binders in the DLR.

  • .Net Binder — this is used to talk to .Net objects
  • JavaScript Binder — this is used to talk to objects created in JavaScript objects
  • IronRuby Binder — enables the DLR to talk to IronRuby objects
  • IronPython Binder — helps the DLR to talk to IronPython objects
  • COM Binder — this helps the DLR to talk to COM objects

The “dynamic” keyword

You can take advantage of the dynamic keyword to access a dynamic object. The dynamic keyword was first introduced in .Net Framework 4. It enables your application to interoperate with dynamic types. So, you can use the dynamic keyword to access a COM object or an object created in dynamic languages like, Python, Ruby or JavaScript.

Here’s is a code snippet that illustrates how the dynamic keyword can be used.

using System.Dynamic;

dynamic excelObj = System.Runtime.InteropServices.Marshal.GetActiveObject(“Excel.Application”);

We no longer need to use reflection to access COM objects – your code is much clean without the reflection code that you would otherwise have had to write sans the dynamic keyword.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author