Metaprogramming in C#

Book description

Developing software can be very hard and often increasingly harder over time. Within the .NET runtime lies a few namespaces that aren’t often mentioned in publications. These namespaces hold treasures for C# and other .NET developers that can help automate tedious tasks and also help in delivering more robust software.

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

  • Write less repetitive code
  • Increase productivity
  • Improve feedback loop
  • Focus on delivering business value
  • Improve security
  • Improve software quality

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.

Table of contents

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

Product information

  • Title: Metaprogramming in C#
  • Author(s): Einar Ingebrigtsen
  • Release date: June 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781837635429