Emitting Dynamic Methods and Assemblies 

This section describes a set of managed types in the System.Reflection.Emit namespace that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) at run time and optionally generate a portable executable (PE) file on disk. Script engines and compilers are the primary users of this namespace. In this section, the functionality provided by the System.Reflection.Emit namespace is referred to as reflection emit.

Reflection emit provides the capabilities:

  • Define lightweight global methods at run time, using the DynamicMethod class, and execute them using delegates.

  • Define assemblies at run time and then run them and/or save them to disk.

  • Define modules in new assemblies at run time and then run and/or save them to disk.

  • Define types in modules at run time, create instances of these types, and invoke their methods.

  • Define symbolic information for defined modules that can be used by tools such as debuggers and code profilers.

In addition to the managed types in the System.Reflection.Emit namespace, there are unmanaged metadata interfaces which are described in the Metadata Interfaces reference documentation. Managed reflection emit provides stronger semantic error checking and a higher level of abstraction of the metadata than the unmanaged metadata interfaces.

Another useful resource for working with metadata and MSIL is the Common Language Infrastructure (CLI) documentation, especially "Partition II: Metadata Definition and Semantics" and "Partition III: CIL Instruction Set". The documentation is available online at https://msdn.microsoft.com/net/ecma/ and http://www.ecma-international.org/publications/standards/Ecma-335.htm.

In This Section

  • Reflection Emit Abstractions
    Lists and describes the reflection emit classes you can use to define assemblies and types, methods and method parameters, events, constructors, properties, and so forth.
  • Reflection Emit Application Scenarios
    Describes the application scenarios supported by reflection emit, including executing a script in a Web browser, executing a script in an ASP.NET page, and compiling a regular expression.
  • Using Reflection Emit
    Describes fundamental tasks you can perform using reflection emit, with How-to topics on generating lightweight dynamic methods and emitting generic types and methods.

Reference

  • OpCodes
    Catalogs the MSIL instruction codes you can use to build method bodies.
  • System.Reflection.Emit
    Contains managed classes used to emit dynamic methods, assemblies, and types.
  • Type
    Describes the Type class, which represents types in managed reflection and reflection emit, and which is key to the use of these technologies.
  • System.Reflection
    Contains managed classes used to explore metadata and managed code.
  • Reflection
    Explains how to explore metadata and managed code.