preface

In software development, metaprogramming is one of those words that sounds fancy and sophisticated—and somewhat intimidating at the same time. But what does it mean to be doing metaprogramming? The meta prefix can mean changed or higher. It can also mean after or beside, depending on the context. All of those terms describe the various forms of metaprogramming that we cover in this book.

You may choose to do metaprogramming in order to change code to support a higher level of abstraction within your system or to inject some new behavior that suits your particular needs. You may choose to do these things at compile time, between compile time and deployment, or even at runtime. Because of the flexible nature of the meta prefix, all of these scenarios qualify as metaprogramming.

No matter your reasons for doing metaprogramming, you must have a firm grip on the larger architectural picture of your project to do it effectively. That’s why metaprogramming is sometimes considered a dark art, to be practiced only by senior developers and architects. Nothing could be further from the truth. Everyone can do some form of metaprogramming. By manipulating code with other code the metaprogramming way, you can suddenly tackle classes of coding problems that you were never able to overcome before.

Your foray into metaprogramming may be to improve code reuse through simple templating or reflection. But soon you might also find yourself doing it to reduce the complexity of your systems. For example, weaving the code that does logging, performance monitoring, or transaction handling into a class library after it’s been compiled can greatly increase developer comprehension by reducing code complexity. Hiding all of that plumbing with metaprogramming can benefit everyone on the team.

We love metaprogramming. We want to create beautiful pieces of code that can enable conventions in applications that make adding a new aspect easy. We want to be able to optimize our code at runtime so it can perform faster. We want to analyze our code so we can find issues before compilation. We want to shape whole bodies of templated code to schemas at runtime, perhaps even compiling them on the fly to get excellent performance. Metaprogramming helps realize all these goals. We’d also like for you to fall in love with metaprogramming so you can reach higher goals. That’s really what we hope to instill in you with this book: a passion to view your code in a different, often more abstract way.

To be fair, it’s not as easy to do metaprogramming in .NET compared to other languages like Ruby. At least it seems that way when you first dive in. Dynamic languages let you easily manipulate your code, and such concepts are exposed as first-class citizens in languages like Python and Ruby. C# and Visual Basic .NET are usually not touted as being dynamic or malleable. Surprisingly, though, there are a lot of ways to do real metaprogramming in .NET. They may not be obvious or easy to carry out at first, but they are there at almost every turn. Some metaprogramming features of .NET are baked into the Common Language Runtime (CLR). Some exist as code in the Framework Class Library (FCL). Still more metaprogramming capabilities show up as language features in C# and Visual Basic .NET. Once you understand how some of these features work, you’ll be well on your way to seeing problems in a whole new light.

Writing this book has been laborious, time-consuming, and frustrating, but above everything else, a joy. As far as we’re concerned, this is the “fun stuff” in software development. It’s also the “stuff” that can truly transform your code into something amazing, as long as you’re willing to stretch your boundaries. So take a deep, cleansing breath and dive in with us. You’ll find that the metaprogramming waters aren’t as choppy as they may seem at first glance. We believe that in the end, you’ll be glad you made the journey.

We also believe that once you’ve mastered a new concept or two, you’ll be ready to convince your peers that the metaprogramming seas are smooth enough for anyone to sail on them.