Chapter 12

Dynamic Language Extensions


  • Understanding the Dynamic Language Runtime
  • The dynamic type
  • The DLR ScriptRuntime
  • Creating dynamic objects with DynamicObject and ExpandoObject


The code downloads for this chapter are found at on the Download Code tab. The code for this chapter is divided into the following major examples:

  • DLRHost
  • Dynamic
  • DynamicFileReader
  • ErrorExample

The growth of languages such as Ruby and Python, and the increased use of JavaScript, have intensified interest in dynamic programming. In previous versions of the .NET Framework, the var keyword and anonymous methods started C# down the “dynamic” road. In version 4, the dynamic type was added. Although C# is still a statically typed language, these additions give it the dynamic capabilities that some developers are looking for.

In this chapter, you’ll look at the dynamic type and the rules for using it. You’ll also see what an implementation of DynamicObject looks like and how it can be used. ExpandoObject, which is the frameworks implementation of DynamicObject, will also be covered.


The dynamic capabilities of C# 4 are part of the dynamic language runtime (DLR). The DLR is a set of services that is added to the common language runtime (CLR) to enable the addition of dynamic languages such as Ruby and Python. It also enables C# to take on some of the same dynamic ...

Get Professional C# 2012 and .NET 4.5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.