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

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

246 45 4MB

English Pages 353 Year 2023

Report DMCA / Copyright

DOWNLOAD EPUB FILE

Table of contents :
Metaprogramming in C#
Contributors
About the author
About the reviewer
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Share Your Thoughts
Download a free PDF copy of this book
Part 1:Why Metaprogramming?
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
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
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
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
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
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
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
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
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
10
Convention over Configuration
Technical requirements
Inversion of control and its role
Refactoring the code
Automatic ServiceCollection registrations by convention
Further refactoring
Composing
Summary
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
12
Go Beyond Inheritance
Technical requirements
Method signature conventions
Infrastructure
Using the infrastructure
Summary
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
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
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
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
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
18
Caveats and Final Words
Performance implications
Hidden magic – handle with care
When to use what
Summary
Index
Why subscribe?
Other Books You May Enjoy
Packt is searching for authors like you
Share Your Thoughts
Download a free PDF copy of this book

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

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
Recommend Papers