Metaprogramming in .NET

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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 youll use to implement them in your .NET code. Youll explore plenty of real-world examples that reinforce key concepts. When you finish, youll 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.

Author(s): Kevin Hazzard, Jason Bock
Edition: Pap/Psc
Publisher: Manning Publications
Year: 2013

Language: English
Pages: 360
Tags: Библиотека;Компьютерная литература;.NET / CLR / CLI;

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