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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.