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:
- 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.
- 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.
- Facilitates sharing of libraries and objects. The DLR enables you to create objects and libraries in one language to be accessed from another language.
- 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:
- Expression trees -- the DLR makes use of expression trees to represent language semantics.
- 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.
- 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.
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
- 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
Here's is a code snippet that illustrates how the dynamic keyword can be used.
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.
This article is published as part of the IDG Contributor Network. Want to Join?