Chapter 20. Dynamic Programming
In Chapter 4, we explained how dynamic binding works in the C# language. In this chapter, we look briefly at the DLR and then explore the following dynamic programming patterns:
Numeric type unification
Dynamic member overload resolution
Custom binding (implementing dynamic objects)
Dynamic language interoperability
In Chapter 25, we’ll describe how
dynamic can improve COM interoperability.
The types in this chapter live in the
System.Dynamic namespace, except for
CallSite<>, which lives in
The Dynamic Language Runtime
C# relies on the dynamic language runtime (DLR) to perform dynamic binding.
Contrary to its name, the DLR is not a dynamic version of the CLR. Rather, it’s a library that sits atop the CLR—just like any other library such as System.Xml.dll. Its primary role is to provide runtime services to unify dynamic programming—in both statically and dynamically typed languages. Hence languages such as C#, VB, IronPython, and IronRuby all use the same protocol for calling functions dynamically. This allows them to share libraries and call code written in other languages.
The DLR also makes it relatively easy to write new dynamic languages in .NET. Instead of having to emit IL, dynamic language authors work at the level of expression trees (the same expression trees in
System.Linq.Expressions that we talked about in Chapter 8).
The DLR further ensures that all consumers get the benefit of