O'Reilly logo

Metaprogramming in .NET by Kevin Hazzard and Jason Bock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required