CHAPTER 16 DYNAMIC TYPES AND THE DYNAMIC LANGUAGE RUNTIME
Although the end result of running this program is identical, this version of the method is much
more verbose, as I am sure you agree. That wraps up our look at the C# dynamic keyword and the DLR.
Hopefully, you can see how these features can simplify complex programming tasks, and (perhaps more
importantly) understand the trade-offs. When you opt into dynamic data, you do lose a good amount of
type safety, and your code base is prone to many more runtime errors.
While there is more to say about the DLR, this chapter has tried to focus on topics that are practical
and useful in your day-to-day programming. If you want to learn more about advanced features of the
Dynamic Language Runtime, such as integrating with scripting languages, be sure to consult the .NET
Framework 4.5 SDK documentation (look up the topic “Dynamic Language Runtime Overview” to get
started).
Source Code The ExportDataToOfficeApp project is included under the Chapter 16 subdirectory.
Summary
The dynamic keyword introduced in C# 4.0 allows you to define data whose true identity is not known
until runtime. When processed by the new Dynamic Language Runtime (DLR), the automatically
created “expression tree” will be passed to the correct dynamic language binder, where the payload will
be unpackaged and sent to the correct object member.
Using dynamic data and the DLR, a number of complex C# programming tasks can be radically
simplified, especially the act of incorporating COM libraries into your .NET applications. As you have
also seen in this chapter, .NET 4.0 and higher provides a number of further simplifications to COM
interop (which have nothing to do with dynamic data), such as embedding COM interop data into your
applications, optional arguments, and named arguments.
While these features can certainly simplify your code, always remember that dynamic data makes
your C# code much less type safe, and open to runtime errors. Be sure you weigh the pros and cons of
using dynamic data in your C# projects, and test accordingly!
621