Metaprogramming in C#: Automate your .NET development and simplify overcomplicated code 1837635420, 9781837635429

Master advanced techniques for dynamic .NET programming and accelerate productivity by automating tasks, generating adap

300 7 10MB

English Pages 352 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

Metaprogramming in C#: Automate your .NET development and simplify overcomplicated code
 1837635420, 9781837635429

Table of contents :
Cover
Title Page
Copyright and Credits
Contributors
Table of Contents
Preface
Part 1: Why Metaprogramming?
Chapter 1: How Can Metaprogramming Benefit You?
Reasoning about your code
Developer concerns
Automation
Metaprogramming
Removing manual structure and process
Maintaining software
Generating code
Compile time safety
Summary
Chapter 2: Metaprogramming Concepts
Technical requirements
Implicit – using what is already there
Leveraging the power of reflection
Explicit – additional adornment of code
Creating custom attributes
Domain-specific languages
Gherkin – technical sample
Summary
Chapter 3: Demystifying through Existing Real-World Examples
Technical requirements
Prerequisites for your system
ASP.NET controllers
Custom HTTP Get handler
Controller
ASP.NET validation
Automatically hooking up model state handling
Summary
Part 2: Leveraging the Runtime
Chapter 4: Reasoning about Types Using Reflection
Technical requirements
Assembly discovery in the running process
Assembly
Leveraging library metadata to get project referenced assemblies
Reusable fundamentals
Business app
Discovering types
Back to business
Domain concepts
Cross-cutting concerns
Open/closed principle applied
Summary
Chapter 5: Leveraging Attributes
Technical requirements
What is an attribute and how can it be applied?
Limiting attribute usage
Sealing your attribute class
Finding types with specific attributes
Personal Identifiable Information (PII)
Generic attributes
Summary
Chapter 6: Dynamic Proxy Generation
Technical requirements
An introduction to IL and Reflection.Emit
Creating a dynamic assembly and module
Virtual members and overrides
Implementing an interface
NotifyObjectWeaver class
Summary
Chapter 7: Reasoning about Expressions
Technical requirements
What are expressions?
Expression
Lambda expression
Traversing an expression tree
Using expressions as descriptors of members on types
Summary
Chapter 8: Building and Executing Expressions
Technical requirements
Creating your own expressions
Creating expressions as delegates and executing them
Creating a query engine
A MongoDB-like database
Building a simple query engine
Summary
Chapter 9: Taking Advantage of the Dynamic Language Runtime
Technical requirements
Understanding the DLR
The CLR at a glance
The DLR building blocks
Call sites and binders
Reasoning about a dynamic type
Creating DynamicObject and providing metadata
Building a JSON schema type
Summary
Part 3: Increasing Productivity, Consistency, and Quality
Chapter 10: Convention over Configuration
Technical requirements
Inversion of control and its role
Refactoring the code
Automatic ServiceCollection registrations by convention
Further refactoring
Composing
Summary
Chapter 11: Applying the Open-Closed Principle
Technical requirements
Encapsulating type discovery
Encapsulating the discovery of instances
Hooking up with the service collection
Practical use case
Helping the developer
Supporting properties
Using the GDPR infrastructure
Adding more providers
Summary
Chapter 12: Go Beyond Inheritance
Technical requirements
Method signature conventions
Infrastructure
Using the infrastructure
Summary
Chapter 13: Applying Cross-Cutting Concerns
Technical requirements
What are cross-cutting concerns?
Leveraging the ASP.NET pipeline
Building consistent result objects
CommandResult
Authorization based on metadata or structure
Summary
Chapter 14: Aspect-Oriented Programming
Technical requirements
What is AOP?
Aspects
Pointcuts
Join points
Logging
Creating a logging sample
Adding the Microsoft logger
Interceptors
Trying out the interceptor
Mixins
Mixing it up
Authorization
Using pointcuts
Summary
Part 4: Compiler Magic Using Roslyn
Chapter 15: Roslyn Compiler Extensions
Technical requirements
How to set up a project and its moving parts
What can they do?
Setting it up
Adding common rules
Common project settings
How to package your extension for reuse
Common package properties
Analyzer
Summary
Chapter 16: Generating Code
Technical requirements
Generating additional code for the Roslyn compiler
ASTs
Application metrics
Improving the developer experience
(Ab)using the compiler to generate not just C# code
Building the generator
Improving the developer experience
Debugging
Optimization
Summary
Chapter 17: Static Code Analysis
Technical requirements
What is static code analysis?
How to write an analyzer
Fleshing out an analyzer
Handling the syntax nodes
Release tracking
Trying out the analyzer
How to write a code fix for an analyzer
How to write automated tests
Analyzer tests
Code fix tests
Summary
Chapter 18: Caveats and Final Words
Performance implications
Hidden magic – handle with care
When to use what
Summary
Index
Other Books You May Enjoy

Polecaj historie