MANNING Kevin Hazzard Jason Bock F OREWORD BY Rockford Lhotka in .NET www.it-ebooks.info Metaprogramming in .NET www.it-ebooks.info www.it-ebooks.info Metaprogramming in .NET KEVIN HAZZARD JASON BOCK MANNING SHELTER ISLAND www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2013 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Cynthia Kane 20 Baldwin Road Copyeditor: Corbin Collins PO Box 261 Technical proofreader: Justin Chase Shelter Island, NY 11964 Proofreader: Elizabeth Martin Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617290268 Printed in the United States of America 12345678910–MAL–18171615141312 www.it-ebooks.info v brief contents PART 1DEMYSTIFYING METAPROGRAMMING 1 1 ■ Metaprogramming concepts 3 2 ■ Exploring code and metadata with reflection 41 P ART 2TECHNIQUES FOR GENERATING CODE 63 3 ■ The Text Template Transformation Toolkit (T4) 65 4 ■ Generating code with the CodeDOM 101 5 ■ Generating code with Reflection.Emit 139 6 ■ Generating code with expressions 171 7 ■ Generating code with IL rewriting 199 P ART 3LANGUAGES AND TOOLS 221 8 ■ The Dynamic Language Runtime 223 9 ■ Languages and tools 267 10 ■ Managing the .NET Compiler 287 www.it-ebooks.info www.it-ebooks.info vii contents foreword xiii preface xv acknowledgments xvii about this book xix about the cover illustration xxiii PART 1DEMYSTIFYING METAPROGRAMMING 1 1 Metaprogramming concepts 3 1.1 Definitions of metaprogramming 6 1.2 Examples of metaprogramming 8 Metaprogramming via scripting 8 ■ Metaprogramming via reflection 11 ■ Metaprogramming via code generation 14 Metaprogramming via dynamic objects 29 1.3 Summary 39 2 Exploring code and metadata with reflection 41 2.1 The need for reflection 42 Creating extensible applications 42 ■ Manipulating code members at runtime 42 www.it-ebooks.info CONTENTS viii 2.2 Reading metadata and executing code 43 Obtaining the starting point 44 ■ Finding member information 46 ■ Gathering attribute data 47 Executing code 48 2.3 Impractical uses of reflection 49 Performance concerns with reflection 49 Brittleness and reflection 50 2.4 Practical uses of reflection 51 Automatically registering known types in WCF 52 Dynamic implementation of ToString 55 Invoking arbitrary methods on objects 58 Quick summary of reflection examples 61 2.5 Summary 62 PART 2TECHNIQUES FOR GENERATING CODE 63 3 The Text Template Transformation Toolkit (T4) 65 3.1 Thinking of generics as templates 66 3.2 Introducing T4 69 T4 syntax basics 71 ■ Understanding T4’s block types 73 How T4 stitches together template blocks 74 ■ T4’s expression control block 75 ■ A brief history of T4 75 3.3 More useful T4 examples 77 Templates should be beautiful 83 3.4 T4 fundamentals 84 Directives and text blocks 84 ■ Control blocks 85 Handling indentation 88 3.5 Using T4 inside Visual Studio 93 How T4 uses the single file generator extension point 93 Creating a T4 template from Visual Studio 95 ■ More on the template directive 96 ■ Using the output directive 97 Using T4 to generate Visual Basic dynamically 98 3.6 Summary 100 4 Generating code with the CodeDOM 101 4.1 Understanding the CodeDOM 102 CodeDOM organization and types 103 ■ How statements and expressions fit together 105 www.it-ebooks.info CONTENTS ix 4.2 The code provider classes 106 Code provider instantiation 106 ■ Code generator supportable options 110 ■ Code provider services 112 4.3 Adding objects to a code graph 113 Creating a namespace with imports 113 ■ Adding a class to a namespace 115 ■ Adding a constructor to a class 116 Adding statements to a member 117 ■ Adding a property to a class 120 4.4 Metaprogramming with the CodeDOM 121 Using branching logic 121 ■ Referencing a member 124 Invoking methods 126 ■ Compiling assemblies 133 Dynamic invocation 135 4.5 Summary 137 5 Generating code with Reflection.Emit 139 5.1 Why Emitter classes? 140 Support for DSLs 140 ■ Moving reflection code into IL 141 Using .NET functionality not supported in your language 142 5.2 An overview of assembly internals 144 Transforming high-level languages 144 ■ Member layouts in assemblies and keywords 147 5.3 A lightning tour of opcodes 148 The mnemonic patterns for opcodes 148 ■ Using local variables 150 ■ Accessing fields 151 ■ Creating objects 152 Calling methods 152 ■ Controlling code flow 153 Exception handling 154 5.4 Creating dynamic assemblies 155 Building a dynamic version of ToString() 155 Adding debugging support 160 ■ Verifying results with peverify 162 ■ Using ILDasm to cheat 164 5.5 Lightweight code generation with dynamic methods 165 When creating an assembly is too much 165 ■ Creating shim methods 166 ■ Using caching for performance 167 Disadvantages of DynamicMethod 168 5.6 Summary 170 6 Generating code with expressions 171 6.1 Expression-oriented programming 172 Understanding code as data 172 ■ Expressions take metaprogramming mainstream 174 www.it-ebooks.info [...]... IL rewriting 199 7.1 The case for code injection 200 Repeated implementations of coding patterns Code restructuring (Code Contracts) 201 7.2 200 Creating an injection framework 203 What’s Cecil? 203 Weaving code with Cecil Creating an MSBuild task 210 ■ 7.3 Debugging injected code 203 212 Clearing up debugging confusion 212 Loading and saving debug information 212 Issues with adding debugging information... Generating the mock code 295 Compiling the mock code 300 Understanding trees 302 ■ ■ ■ ■ 10.3 Interacting with code in Visual Studio 303 Creating a IsOneWay warning 303 Defining the Code Issue 303 Defining the OneWayOperation code actions 306 Viewing the results 307 Autoarrange code 309 Specifying the algorithm to reformat the code 309 Defining the core parts of the refactoring project 310 Creating a... imitation is the sincerest form of flattery Let’s begin by defining what metaprogramming is Then we’ll dive into a few interesting examples to show how it’s used www.it-ebooks.info 6 1.1 CHAPTER 1 Metaprogramming concepts Definitions of metaprogramming The classic definition for a metaprogram is “a computer program that writes new computer programs.” This sounds a lot like the definition of a compiler... the need for reflection and its practical uses Numerous uses of metaprogramming via the Reflection API will be presented throughout the chapter www.it-ebooks.info www.it-ebooks.info Metaprogramming concepts In this chapter ■ Defining metaprogramming ■ Exploring examples of metaprogramming The basic principles of object-oriented programming (OOP) are understood by most software developers these days... Microsoft’s compilers used in many metaprogramming scenarios Metaprogramming may be among the most misunderstood terms in computer jargon It’s certainly one of the more difficult to define To make learning about it easier, each time you see the word metaprogramming in this book, try to think of it as after-programming or beside-programming The Greek prefix meta allows for both of those definitions to be correct... illustrations such as this one xxiii www.it-ebooks.info www.it-ebooks.info Part 1 Demystifying metaprogramming W hat is metaprogramming? What does it look like? What does it mean to use metaprogramming? Part 1 (chapters 1 and 2) gives you a tour of the foundations of metaprogramming In chapter 1 you’ll see simple, clear examples that explain what metaprogramming is and why it’s beneficial to understand what... vantage point, DSLs are precious to the businesses they serve precisely because they lower perceived complexity In the end, isn’t that what we do for a living? We make difficult business problems seem simple As you study metaprogramming throughout this book, keep that thought in mind DSLs in Action by Debasish Ghosh (www.manning.com/ghosh) and DSLs in Boo by Oren Eini writing as Ayende Rahien (www.manning.com/... Most of the examples in this book demonstrate programming after traditional compilation has occurred, or by using dynamic code that runs alongside other processes For each example, ask yourself which kind of metaprogramming you’re observing Some of the more in- depth examples demonstrate both kinds simultaneously DEFINITION Also inherent in the classic definition of metaprogramming is the notion that... www.it-ebooks.info Definitions of metaprogramming 7 Another common definition for metaprogramming is “a computer program that manipulates other programs at runtime.” Scripting languages often fit this mold, providing the simple but powerful tools for doing metaprogramming A program that manipulates another program doesn’t have to be a scripting language The dynamic keyword in C# can be used to emit a kind of... runtime binder using the literal string "Open." When you dig around in the Intermediate Language (IL) emitted by the compiler for the preceding snippet, you may be surprised to see the literal string "Open" passed to the binder to invoke the method The C# code certainly didn’t look like a scripting language, but what was emitted certainly has that flavor Through the various runtime binders for interfacing . MANNING Kevin Hazzard Jason Bock F OREWORD BY Rockford Lhotka in .NET www.it-ebooks.info Metaprogramming in .NET www.it-ebooks.info www.it-ebooks.info Metaprogramming in .NET KEVIN HAZZARD JASON. Definitions of metaprogramming 6 1.2 Examples of metaprogramming 8 Metaprogramming via scripting 8 ■ Metaprogramming via reflection 11 ■ Metaprogramming via code generation 14 Metaprogramming. 42 www.it-ebooks.info CONTENTS viii 2.2 Reading metadata and executing code 43 Obtaining the starting point 44 ■ Finding member information 46 ■ Gathering attribute data 47 Executing code 48 2.3 Impractical