Chapter 19. Dynamic Programming
Chapter 4 explained how dynamic binding works in the C# language. In this chapter, we look briefly at the Dynamic Language Runtime (DLR) and then explore the following dynamic programming patterns:
Numeric type unification
Dynamic member overload resolution
Custom binding (implementing dynamic objects)
Dynamic language interoperability
Note
In Chapter 24, we describe how dynamic
can improve COM interoperability.
The types in this chapter reside in the System.Dynamic
namespace, except for CallSite<>
, which resides in System.Runtime.CompilerServices
.
The Dynamic Language Runtime
C# relies on the 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#, Visual Basic, 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 Intermediate Language (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 C# 10 in a Nutshell 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.