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

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"

Master advanced techniques for dynamic .NET programming and accelerate productivity by automating tasks, generating adaptable code, and more Purchase of the print or Kindle book includes a free PDF eBook Key Features Employ metaprogramming to automate your tasks and increase your productivity Write maintainable, scalable, and adaptable code using metaprogramming techniques Leverage the .NET runtime for complex problem-solving Book Description 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. What you will learn Explore how to leverage the .NET runtime Improve code quality and increase productivity Write adaptable code for changing requirements Learn Roslyn for code generation and static analysis Master metaprogramming and its practical implementations Use Dynamic Language Runtime for flexible and expressive programming Who this book is for 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.

Author(s): Einar Ingebrigtsen
Publisher: Packt Publishing Pvt Ltd
Year: 2023

Language: English
Pages: 353

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