Chapter 5. Generating code with Reflection.Emit

This chapter covers

  • Scenarios for generating code at runtime
  • A quick overview of opcodes
  • Building code with Reflection.Emit and DynamicMethod

In chapter 4, you saw how you can generate code via the CodeDOM. Another option in .NET lets you do the same thing, except it uses IL directly to create the code at runtime. This provides a substantial performance boost and access to any feature supported by the CLR. All the supporting classes exist in the System.Reflection.Emit namespace, and that’s where you’ll spend your time in this chapter. We cover how the common opcodes work, and then you’ll see examples that generate dynamic assemblies and methods.

The first thing you need to understand is why ...

Get Metaprogramming in .NET now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.