Chapter 2. Exploring code and metadata with reflection
This chapter covers
- The need for reflection
- Reading metadata and executing code
- Practical uses of reflection
Metaprogramming in .NET can incorporate many different concepts and techniques. Some areas are fairly easy to grasp, others can be quite difficult. The best way to begin is to visit the Reflection API, which has been in .NET since version 1. It provides a fairly simplistic introduction to core concepts of metaprogramming (such as introspection) and it gives you a glimpse into the structure of .NET code.
To start the journey down the .NET metaprogramming road, you’ll go through a couple of problems where reflection helps solve the issue. You’ll walk through examples that demonstrate reading the contents of an assembly and running its code. Finally, you’ll dissect real-world examples that we’ve used on projects in the past which illustrate how useful reflection can be in creating generalized, compact solutions. Let’s start by looking at a couple metaprogramming-related issues you can solve with the power of the Reflection API.