**Summary** __Metaprogramming in .NET__ is designed to help readers understand the basic concepts, advantages, and potential pitfalls of metaprogramming. It introduces core concepts in clear, easy-to-follow language and then it takes you on a deep dive into the tools and techniques you'll use to implement them in your .NET code. You'll explore plenty of real-world examples that reinforce key concepts. When you finish, you'll be able to build high-performance, metaprogramming-enabled software with confidence. **About the Technology** When you write programs that create or modify other programs, you are metaprogramming. In .NET, you can use reflection as well as newer concepts like code generation and scriptable software. The emerging Roslyn project exposes the .NET compiler as an interactive API, allowing compile-time code analysis and just-in-time refactoring. **About this Book** __Metaprogramming in .NET__ is a practical introduction to the use of metaprogramming to improve the performance and maintainability of your code. This book avoids abstract theory and instead teaches you solid practices you'll find useful immediately. It introduces core concepts like code generation and application composition in clear, easy-to-follow language. Written for readers comfortable with C# and the .NET framework—no prior experience with metaprogramming is required. Purchase of the print book comes with an offer of a free PDF, ePub, and Kindle eBook from Manning. Also available is all code from the book. **What's Inside** * Metaprogramming concepts in plain language * Creating scriptable software * Code generation techniques * The Dynamic Language Runtime **About the Authors** **Kevin Hazzard** is a Microsoft MVP, consultant, teacher, and developer community leader in the mid-Atlantic USA. **Jason Bock** is an author, Microsoft MVP, and the leader of the Twin Cities Code Camp. __"An excellent way to start fully using the power of metaprogramming."__—From the Foreword by Rockford Lhotka, Creator of the CSLA .NET Framework **Table of Contents** PART 1 DEMYSTIFYING METAPROGRAMMING 2. Metaprogramming concepts 3. Exploring code and metadata with reflection PART 2 TECHNIQUES FOR GENERATING CODE 5. The Text Template Transformation Toolkit (T4) 6. Generating code with the CodeDOM 7. Generating code with Reflection.Emit 8. Generating code with expressions 9. Generating code with IL rewriting PART 3 LANGUAGES AND TOOLS 11. The Dynamic Language Runtime 12. Languages and tools 13. Managing the .NET Compiler Front cover......Page 1 brief contents......Page 6 contents......Page 8 foreword......Page 14 preface......Page 16 acknowledgments......Page 18 Roadmap......Page 20 Code conventions and downloads......Page 21 About the authors......Page 22 about the cover illustration......Page 24 Part 1—Demystifying metaprogramming......Page 26 1 Metaprogramming concepts......Page 28 1.1 Definitions of metaprogramming......Page 31 1.2.1 Metaprogramming via scripting......Page 33 1.2.2 Metaprogramming via reflection......Page 36 1.2.3 Metaprogramming via code generation......Page 39 1.2.4 Metaprogramming via dynamic objects......Page 54 1.3 Summary......Page 64 2 Exploring code and metadata with reflection......Page 66 2.1.2 Manipulating code members at runtime......Page 67 2.2 Reading metadata and executing code......Page 68 2.2.1 Obtaining the starting point......Page 69 2.2.2 Finding member information......Page 71 2.2.3 Gathering attribute data......Page 72 2.2.4 Executing code......Page 73 2.3.1 Performance concerns with reflection......Page 74 2.3.2 Brittleness and reflection......Page 75 2.4 Practical uses of reflection......Page 76 2.4.1 Automatically registering known types in WCF......Page 77 2.4.2 Dynamic implementation of ToString......Page 80 2.4.3 Invoking arbitrary methods on objects......Page 83 2.4.4 Quick summary of reflection examples......Page 86 2.5 Summary......Page 87 Part 2—Techniques for generating code......Page 88 3 The Text Template Transformation Toolkit (T4)......Page 90 3.1 Thinking of generics as templates......Page 91 3.2 Introducing T4......Page 94 3.2.1 T4 syntax basics......Page 96 3.2.2 Understanding T4’s block types......Page 98 3.2.3 How T4 stitches together template blocks......Page 99 3.2.5 A brief history of T4......Page 100 3.3 More useful T4 examples......Page 102 3.3.1 Templates should be beautiful......Page 108 3.4.1 Directives and text blocks......Page 109 3.4.2 Control blocks......Page 110 3.4.3 Handling indentation......Page 113 3.5.1 How T4 uses the single file generator extension point......Page 118 3.5.2 Creating a T4 template from Visual Studio......Page 120 3.5.3 More on the template directive......Page 121 3.5.4 Using the output directive......Page 122 3.5.5 Using T4 to generate Visual Basic dynamically......Page 123 3.6 Summary......Page 125 4 Generating code with the CodeDOM......Page 126 4.1 Understanding the CodeDOM......Page 127 4.1.1 CodeDOM organization and types......Page 128 4.1.2 How statements and expressions fit together......Page 130 4.2.1 Code provider instantiation......Page 131 4.2.2 Code generator supportable options......Page 135 4.2.3 Code provider services......Page 137 4.3.1 Creating a namespace with imports......Page 138 4.3.2 Adding a class to a namespace......Page 140 4.3.3 Adding a constructor to a class......Page 141 4.3.4 Adding statements to a member......Page 142 4.3.5 Adding a property to a class......Page 145 4.4.1 Using branching logic......Page 146 4.4.2 Referencing a member......Page 149 4.4.3 Invoking methods......Page 151 4.4.4 Compiling assemblies......Page 158 4.4.5 Dynamic invocation......Page 160 4.5 Summary......Page 162 5 Generating code with Reflection.Emit......Page 164 5.1.1 Support for DSLs......Page 165 5.1.2 Moving reflection code into IL......Page 166 5.1.3 Using .NET functionality not supported in your language......Page 167 5.2.1 Transforming high-level languages......Page 169 5.2.2 Member layouts in assemblies and keywords......Page 172 5.3.1 The mnemonic patterns for opcodes......Page 173 5.3.2 Using local variables......Page 175 5.3.3 Accessing fields......Page 176 5.3.5 Calling methods......Page 177 5.3.6 Controlling code flow......Page 178 5.3.7 Exception handling......Page 179 5.4.1 Building a dynamic version of ToString()......Page 180 5.4.2 Adding debugging support......Page 185 5.4.3 Verifying results with peverify......Page 187 5.4.4 Using ILDasm to cheat......Page 189 5.5.1 When creating an assembly is too much......Page 190 5.5.2 Creating shim methods......Page 191 5.5.3 Using caching for performance......Page 192 5.5.4 Disadvantages of DynamicMethod......Page 193 5.6 Summary......Page 195 6 Generating code with expressions......Page 196 6.1.1 Understanding code as data......Page 197 6.1.2 Expressions take metaprogramming mainstream......Page 199 6.2.1 Understanding LINQ Expressions......Page 201 6.2.2 Generating expressions at runtime......Page 203 6.2.3 Comparison with dynamic methods......Page 207 6.3.1 Debugging expressions......Page 209 6.3.2 Mutating expression trees......Page 212 6.4.1 The essence of genetic programming......Page 214 6.4.2 Applying GAs to expressions......Page 216 6.5 Summary......Page 223 7 Generating code with IL rewriting......Page 224 7.1.1 Repeated implementations of coding patterns......Page 225 7.1.2 Code restructuring (Code Contracts)......Page 226 7.2.2 Weaving code with Cecil......Page 228 7.2.3 Creating an MSBuild task......Page 235 7.3.2 Loading and saving debug information......Page 237 7.3.3 Issues with adding debugging information......Page 238 7.3.4 Adding debugging information for injected code......Page 239 7.4 Summary......Page 243 Part 3—Languages and tools......Page 246 8 The Dynamic Language Runtime......Page 248 8.1.1 The ExpandoObject class......Page 249 8.1.2 The DynamicObject class......Page 252 8.1.3 Parsing the Open Data Protocol dynamically......Page 256 8.2 The DLR hosting model......Page 264 8.2.1 Runtimes, engines, and scopes......Page 266 8.2.2 Adding a rules engine to your application......Page 277 8.3 Summary......Page 289 9 Languages and tools......Page 292 9.1.1 C# and expression limitations......Page 293 9.1.2 Boo and metaprogramming......Page 294 9.1.3 Nemerle and metaprogrammg......Page 300 9.2.1 What is Spring.NET?......Page 302 9.2.2 Intercepting property usage with Spring.NET......Page 303 9.2.4 Intercepting object creation with PostSharp......Page 305 9.2.5 Implementing Equals() and GetHashCode()......Page 307 9.2.6 A quick dive into the internals of PostSharp......Page 310 9.3 Summary......Page 311 10 Managing the .NET Compiler......Page 312 10.1.2 Limitations for metaprogramming......Page 313 10.1.3 What Roslyn provides: a white box......Page 314 10.1.4 What’s in (and not in) the CTP......Page 315 10.2.1 Running code snippets with the script engine......Page 316 10.2.3 What is a mock?......Page 319 10.2.4 Generating the mock code......Page 320 10.2.5 Compiling the mock code......Page 325 10.2.6 Understanding trees......Page 327 10.3.2 Defining the Code Issue......Page 328 10.3.3 Defining the OneWayOperation code actions......Page 331 10.3.4 Viewing the results......Page 332 10.3.6 Specifying the algorithm to reformat the code......Page 334 10.3.7 Defining the core parts of the refactoring project......Page 335 10.3.8 Creating a code action......Page 336 10.3.9 Viewing the results......Page 339 10.4 Summary......Page 340 A.1 The limits of emitting code......Page 341 A.2 Expressions are supported......Page 342 A.3 Changes with Reflection......Page 343 appendix B Usage guide......Page 344 A......Page 346 C......Page 347 D......Page 349 E......Page 350 G......Page 351 I......Page 352 M......Page 353 N......Page 354 R......Page 355 S......Page 356 T......Page 357 V......Page 358 Y......Page 359 Back cover......Page 360 Front cover 1 brief contents 6 contents 8 foreword 14 preface 16 acknowledgments 18 about this book 20 Roadmap 20 Who should read this book? 21 Code conventions and downloads 21 Author Online 22 About the authors 22 about the cover illustration 24 Part 1—Demystifying metaprogramming 26 1 Metaprogramming concepts 28 1.1 Definitions of metaprogramming 31 1.2 Examples of metaprogramming 33 1.2.1 Metaprogramming via scripting 33 1.2.2 Metaprogramming via reflection 36 1.2.3 Metaprogramming via code generation 39 1.2.4 Metaprogramming via dynamic objects 54 1.3 Summary 64 2 Exploring code and metadata with reflection 66 2.1 The need for reflection 67 2.1.1 Creating extensible applications 67 2.1.2 Manipulating code members at runtime 67 2.2 Reading metadata and executing code 68 2.2.1 Obtaining the starting point 69 2.2.2 Finding member information 71 2.2.3 Gathering attribute data 72 2.2.4 Executing code 73 2.3 Impractical uses of reflection 74 2.3.1 Performance concerns with reflection 74 2.3.2 Brittleness and reflection 75 2.4 Practical uses of reflection 76 2.4.1 Automatically registering known types in WCF 77 2.4.2 Dynamic implementation of ToString 80 2.4.3 Invoking arbitrary methods on objects 83 2.4.4 Quick summary of reflection examples 86 2.5 Summary 87 Part 2—Techniques for generating code 88 3 The Text Template Transformation Toolkit (T4) 90 3.1 Thinking of generics as templates 91 3.2 Introducing T4 94 3.2.1 T4 syntax basics 96 3.2.2 Understanding T4’s block types 98 3.2.3 How T4 stitches together template blocks 99 3.2.4 T4’s expression control block 100 3.2.5 A brief history of T4 100 3.3 More useful T4 examples 102 3.3.1 Templates should be beautiful 108 3.4 T4 fundamentals 109 3.4.1 Directives and text blocks 109 3.4.2 Control blocks 110 3.4.3 Handling indentation 113 3.5 Using T4 inside Visual Studio 118 3.5.1 How T4 uses the single file generator extension point 118 3.5.2 Creating a T4 template from Visual Studio 120 3.5.3 More on the template directive 121 3.5.4 Using the output directive 122 3.5.5 Using T4 to generate Visual Basic dynamically 123 3.6 Summary 125 4 Generating code with the CodeDOM 126 4.1 Understanding the CodeDOM 127 4.1.1 CodeDOM organization and types 128 4.1.2 How statements and expressions fit together 130 4.2 The code provider classes 131 4.2.1 Code provider instantiation 131 4.2.2 Code generator supportable options 135 4.2.3 Code provider services 137 4.3 Adding objects to a code graph 138 4.3.1 Creating a namespace with imports 138 4.3.2 Adding a class to a namespace 140 4.3.3 Adding a constructor to a class 141 4.3.4 Adding statements to a member 142 4.3.5 Adding a property to a class 145 4.4 Metaprogramming with the CodeDOM 146 4.4.1 Using branching logic 146 4.4.2 Referencing a member 149 4.4.3 Invoking methods 151 4.4.4 Compiling assemblies 158 4.4.5 Dynamic invocation 160 4.5 Summary 162 5 Generating code with Reflection.Emit 164 5.1 Why Emitter classes? 165 5.1.1 Support for DSLs 165 5.1.2 Moving reflection code into IL 166 5.1.3 Using .NET functionality not supported in your language 167 5.2 An overview of assembly internals 169 5.2.1 Transforming high-level languages 169 5.2.2 Member layouts in assemblies and keywords 172 5.3 A lightning tour of opcodes 173 5.3.1 The mnemonic patterns for opcodes 173 5.3.2 Using local variables 175 5.3.3 Accessing fields 176 5.3.4 Creating objects 177 5.3.5 Calling methods 177 5.3.6 Controlling code flow 178 5.3.7 Exception handling 179 5.4 Creating dynamic assemblies 180 5.4.1 Building a dynamic version of ToString() 180 5.4.2 Adding debugging support 185 5.4.3 Verifying results with peverify 187 5.4.4 Using ILDasm to cheat 189 5.5 Lightweight code generation with dynamic methods 190 5.5.1 When creating an assembly is too much 190 5.5.2 Creating shim methods 191 5.5.3 Using caching for performance 192 5.5.4 Disadvantages of DynamicMethod 193 5.6 Summary 195 6 Generating code with expressions 196 6.1 Expression-oriented programming 197 6.1.1 Understanding code as data 197 6.1.2 Expressions take metaprogramming mainstream 199 6.2 Making dynamic methods with LINQ Expressions 201 6.2.1 Understanding LINQ Expressions 201 6.2.2 Generating expressions at runtime 203 6.2.3 Comparison with dynamic methods 207 6.3 Using expressions effectively 209 6.3.1 Debugging expressions 209 6.3.2 Mutating expression trees 212 6.4 Evolving expression trees 214 6.4.1 The essence of genetic programming 214 6.4.2 Applying GAs to expressions 216 6.5 Summary 223 7 Generating code with IL rewriting 224 7.1 The case for code injection 225 7.1.1 Repeated implementations of coding patterns 225 7.1.2 Code restructuring (Code Contracts) 226 7.2 Creating an injection framework 228 7.2.1 What’s Cecil? 228 7.2.2 Weaving code with Cecil 228 7.2.3 Creating an MSBuild task 235 7.3 Debugging injected code 237 7.3.1 Clearing up debugging confusion 237 7.3.2 Loading and saving debug information 237 7.3.3 Issues with adding debugging information 238 7.3.4 Adding debugging information for injected code 239 7.4 Summary 243 Part 3—Languages and tools 246 8 The Dynamic Language Runtime 248 8.1 The simplest dynamic classes 249 8.1.1 The ExpandoObject class 249 8.1.2 The DynamicObject class 252 8.1.3 Parsing the Open Data Protocol dynamically 256 8.2 The DLR hosting model 264 8.2.1 Runtimes, engines, and scopes 266 8.2.2 Adding a rules engine to your application 277 8.3 Summary 289 9 Languages and tools 292 9.1 A survey of languages 293 9.1.1 C# and expression limitations 293 9.1.2 Boo and metaprogramming 294 9.1.3 Nemerle and metaprogrammg 300 9.2 A survey of tools 302 9.2.1 What is Spring.NET? 302 9.2.2 Intercepting property usage with Spring.NET 303 9.2.3 What is PostSharp? 305 9.2.4 Intercepting object creation with PostSharp 305 9.2.5 Implementing Equals() and GetHashCode() 307 9.2.6 A quick dive into the internals of PostSharp 310 9.3 Summary 311 10 Managing the .NET Compiler 312 10.1 Opening up the compiler 313 10.1.1 The current state of affairs: a black box 313 10.1.2 Limitations for metaprogramming 313 10.1.3 What Roslyn provides: a white box 314 10.1.4 What’s in (and not in) the CTP 315 10.2 Understanding the basics of Roslyn 316 10.2.1 Running code snippets with the script engine 316 10.2.2 Creating dynamic assemblies with Roslyn 319 10.2.3 What is a mock? 319 10.2.4 Generating the mock code 320 10.2.5 Compiling the mock code 325 10.2.6 Understanding trees 327 10.3 Interacting with code in Visual Studio 328 10.3.1 Creating a IsOneWay warning 328 10.3.2 Defining the Code Issue 328 10.3.3 Defining the OneWayOperation code actions 331 10.3.4 Viewing the results 332 10.3.5 Autoarrange code 334 10.3.6 Specifying the algorithm to reformat the code 334 10.3.7 Defining the core parts of the refactoring project 335 10.3.8 Creating a code action 336 10.3.9 Viewing the results 339 10.4 Summary 340 appendix A Metaprogramming in Windows 8 341 A.1 The limits of emitting code 341 A.2 Expressions are supported 342 A.3 Changes with Reflection 343 appendix B Usage guide 344 index 346 Symbols 346 A 346 B 347 C 347 D 349 E 350 F 351 G 351 H 352 I 352 J 353 K 353 L 353 M 353 N 354 O 355 P 355 Q 355 R 355 S 356 T 357 U 358 V 358 W 359 X 359 Y 359 Back cover 360 Metaprogramming in .NET is a practical introduction to the use of metaprogramming to improve the performance and maintainability of your code. This book avoids abstract theory and instead teaches you solid practices you'll find useful immediately. It introduces core concepts like code generation and application composition in clear, easy-to-follow language, and then it takes you on a deep dive into the tools and techniques that will help you implement them in your .NET applications. Written for readers comfortable with C# and the .NET framework--no prior experience with metaprogramming is required
Metaprogramming in.NET is designed to help readers understand the basic concepts, advantages and potential pitfalls of metaprogramming. It introduces core concepts in clear, easy-to-follow language and then it takes you on a deep dive into the tools and techniques you'll use to implement them in your.NET code. You'll explore plenty of real-world examples that reinforce key concepts. When you finish, you'll be able to build high-performance, metaprogramming-enabled software with confidence.