Chapter 4. Generating code with the CodeDOM

 

This chapter covers

  • Understanding the CodeDOM
  • The code providers classes
  • Adding objects to a code graph
  • Metaprogramming with the CodeDOM

Web browsers have a Document Object Model (DOM) for creating windows and managing the navigation between pages. HTML has a DOM for describing the content and structure of those pages. JavaScript has a DOM for automating the others. These models are appropriately called DOMs because they’re technologies for the World Wide Web, which is a largely document-oriented system.

Given the range of roles that a DOM can fill, what do you think Microsoft’s CodeDOM does? It could be used to describe code. As you learned in chapters 1 and 2, .NET provides a rich metadata foundation for applications. Or perhaps it’s used to define code as data in a sort of document outline fashion. An API so promisingly named might also be used to generate code. After all, JavaScript can generate HTML on the fly. Why shouldn’t the CodeDOM be able to generate .NET code dynamically?

As you might have guessed by our hypothesizing about the nature of the API, the CodeDOM and the underlying code providers that you’ll learn about in this chapter enable all these scenarios in .NET:

  • Describing code in a mostly language-independent data structure
  • Generating source code in a variety of languages
  • Compiling code and code-as-data into .NET assemblies

4.1. Understanding the CodeDOM

4.2. The code provider classes

4.3. Adding objects to a code graph

4.4. Metaprogramming with the CodeDOM

4.5. Summary

sitemap