Cover....1
Title Page....2
Copyright and Credits....3
Contributors....4
Table of Contents....6
Preface....12
Part 1: Why Metaprogramming?....18
Chapter 1: How Can Metaprogramming Benefit You?....20
Reasoning about your code....20
Developer concerns....21
Automation....22
Metaprogramming....22
Removing manual structure and process....24
Maintaining software....25
Generating code....25
Compile time safety....27
Summary....28
Chapter 2: Metaprogramming Concepts....30
Technical requirements....30
Implicit – using what is already there....31
Leveraging the power of reflection....32
Explicit – additional adornment of code....35
Creating custom attributes....36
Domain-specific languages....38
Gherkin – technical sample....39
Summary....41
Chapter 3: Demystifying through Existing Real-World Examples....42
Technical requirements....42
Prerequisites for your system....43
ASP.NET controllers....43
Custom HTTP Get handler....44
Controller....46
ASP.NET validation....47
Automatically hooking up model state handling....50
Summary....52
Part 2: Leveraging the Runtime....54
Chapter 4: Reasoning about Types Using Reflection....56
Technical requirements....56
Assembly discovery in the running process....57
Assembly....57
Leveraging library metadata to get project referenced assemblies....58
Reusable fundamentals....59
Business app....62
Discovering types....63
Back to business....64
Domain concepts....66
Cross-cutting concerns....68
Open/closed principle applied....70
Summary....71
Chapter 5: Leveraging Attributes....72
Technical requirements....72
What is an attribute and how can it be applied?....72
Limiting attribute usage....74
Sealing your attribute class....76
Finding types with specific attributes....77
Personal Identifiable Information (PII)....77
Generic attributes....81
Summary....83
Chapter 6: Dynamic Proxy Generation....84
Technical requirements....84
An introduction to IL and Reflection.Emit....84
Creating a dynamic assembly and module....85
Virtual members and overrides....91
Implementing an interface....92
NotifyObjectWeaver class....94
Summary....105
Chapter 7: Reasoning about Expressions....106
Technical requirements....106
What are expressions?....107
Expression....109
Lambda expression....111
Traversing an expression tree....112
Using expressions as descriptors of members on types....115
Summary....118
Chapter 8: Building and Executing Expressions....120
Technical requirements....120
Creating your own expressions....121
Creating expressions as delegates and executing them....122
Creating a query engine....123
A MongoDB-like database....124
Building a simple query engine....125
Summary....132
Chapter 9: Taking Advantage of the Dynamic Language Runtime....134
Technical requirements....134
Understanding the DLR....135
The CLR at a glance....135
The DLR building blocks....136
Call sites and binders....138
Reasoning about a dynamic type....138
Creating DynamicObject and providing metadata....141
Building a JSON schema type....142
Summary....150
Part 3: Increasing Productivity, Consistency, and Quality....152
Chapter 10: Convention over Configuration....154
Technical requirements....154
Inversion of control and its role....155
Refactoring the code....160
Automatic ServiceCollection registrations by convention....166
Further refactoring....167
Composing....171
Summary....175
Chapter 11: Applying the Open-Closed Principle....178
Technical requirements....179
Encapsulating type discovery....180
Encapsulating the discovery of instances....182
Hooking up with the service collection....183
Practical use case....184
Helping the developer....187
Supporting properties....189
Using the GDPR infrastructure....191
Adding more providers....195
Summary....197
Chapter 12: Go Beyond Inheritance....198
Technical requirements....198
Method signature conventions....198
Infrastructure....202
Using the infrastructure....211
Summary....215
Chapter 13: Applying Cross-Cutting Concerns....216
Technical requirements....216
What are cross-cutting concerns?....217
Leveraging the ASP.NET pipeline....218
Building consistent result objects....218
CommandResult....220
Authorization based on metadata or structure....229
Summary....240
Chapter 14: Aspect-Oriented Programming....242
Technical requirements....242
What is AOP?....242
Aspects....243
Pointcuts....243
Join points....244
Logging....245
Creating a logging sample....246
Adding the Microsoft logger....247
Interceptors....249
Trying out the interceptor....252
Mixins....255
Mixing it up....256
Authorization....262
Using pointcuts....263
Summary....269
Part 4: Compiler Magic Using Roslyn....270
Chapter 15: Roslyn Compiler Extensions....272
Technical requirements....272
How to set up a project and its moving parts....272
What can they do?....273
Setting it up....274
Adding common rules....276
Common project settings....278
How to package your extension for reuse....280
Common package properties....281
Analyzer....282
Summary....284
Chapter 16: Generating Code....286
Technical requirements....287
Generating additional code for the Roslyn compiler....287
ASTs....287
Application metrics....288
Improving the developer experience....292
(Ab)using the compiler to generate not just C# code....305
Building the generator....306
Improving the developer experience....311
Debugging....311
Optimization....312
Summary....312
Chapter 17: Static Code Analysis....314
Technical requirements....315
What is static code analysis?....315
How to write an analyzer....316
Fleshing out an analyzer....316
Handling the syntax nodes....319
Release tracking....320
Trying out the analyzer....322
How to write a code fix for an analyzer....324
How to write automated tests....327
Analyzer tests....328
Code fix tests....330
Summary....332
Chapter 18: Caveats and Final Words....334
Performance implications....334
Hidden magic – handle with care....335
When to use what....336
Summary....337
Index....338
Other Books You May Enjoy....347
Metaprogramming is an advanced technique that helps developers to automate repetitive tasks, generate scalable code, and enhance productivity in software development. Metaprogramming in C# is a comprehensive guide that will help you reap the full potential of metaprogramming in .NET runtime.
You'll start by learning about the .NET runtime environment and how you can use it to become a more productive developer. You'll learn how to infer types using reflection, use attributes, and create dynamic proxies. You'll also explore the use of expressions to create and execute code and how to take advantage of Dynamic Language Runtime.
But that's not all! You'll also learn to go beyond inheritance and use method signature conventions to create easily maintainable code. Finally, you'll dive into the world of compiler magic with Roslyn, where you'll discover how to use Roslyn to generate code, perform static code analysis, and write your own compiler extensions.
By the end of this book, you'll have a deep understanding of metaprogramming concepts and how to apply them to your C# code. You'll be able to think about types, use attributes and expressions to generate code, and apply crosscutting concerns to improve code quality.
This book is for C# developers interested in learning about the .NET runtime and how to leverage it for writing maintainable, scalable, and secure code. Software architects who are responsible for designing and managing complex software solutions will also benefit from the book.