An introduction to IL and Reflection.Emit
In Chapter 2, Metaprogramming Concepts, we touched on what the C# compiler turns your code into. IL, short for Intermediate Language, is a representation of instructions the .NET runtime understands and translates to CPU instructions for the target CPU your code is running on.
Since the .NET runtime operates dynamically on your code in this manner, it means that it is not a too far a jump to conclude that you should be able to generate code as your program executes. And luckily, that is the case. The .NET APIs include a whole namespace dedicated to generating code – System.Reflection.Emit.
With the Emit APIs, you can go and create any constructs you want from scratch, introduce new types that do not exist in any source code, or create new types that inherit from other types and add new capabilities.
For all the different types of artifacts, you can create class, method, properties, and more. There are specific builder types...