The Reflection.Emit namespace classes can be used to emit Microsoft intermediate language (MSIL) code on the fly so that the generated code can be executed directly. Reflection is also used to obtain information about a class and its members. In other words, reflection is a technology that allows you to examine metadata that describes types and their members. You may have programmatically accessed Component Object Model type libraries before; reflection in .NET is very similar, but it is a lot more powerful and a lot easier to use. When you compile a source file with a .NET compiler, the compiler emits the MSIL of the statements within the source file, along with the metadata that describes the types defined within the file. It is this metadata that the reflection APIs in .NET enable you to examine. The MSIL and metadata are contained in assembly files (typically .dll and .exe files).
The reflection API in .NET uses the System.Reflection namespace. In this namespace are classes used to help you access structures inherent in a program such as classes, types, fields, structs, enums, members, and methods. For example, you use the Type class to identify the type of the class being reflected, and the FieldInfo class represents the fields of a struct or enum. The MemberInfo class represents the members of the reflected class, and you use the MethodInfo class to represent methods of the reflected class. The ParameterInfo class represents the parameters of a method in the reflected class.
You can create code at runtime with the System.Reflection.Emit namespace classes, but we must warn you that you must have some knowledge of the MSIL operation codes and the MSIL language to take advantage of the emitting commands. In fact, what you are actually emitting is the MSIL itself behind the scenes. You can use reflection to define an assembly in memory, create a class/module for that assembly, and then create other module members plus new types for that module. You can construct the assembly using emitting. Reflection.Emit is a powerful namespace in which we can dynamically emit transient and persisting assemblies at runtime. Reflection.Emit produces a low-level, language-neutral MSIL. Normally, we create an assembly by saving its source code to disk and then compiling that source code. Then we call the methods of classes that we need to use from that assembly, which was compiled on disk. But as you can imagine, this involves extra disk write and read effort! With reflection emit, we can omit this overhead and immediately emit the operation codes directly into memory. Reflection emitting is nothing but writing any assembly code directly within your code and then invoking the resulting code on the fly.
You should use Reflection.Emit members for the following reasons:
- You have your own macro languages, compilers, or script compilers in your applications.
- You want to improve performance of your algorithms by creating assemblies, classes, modules, and new types during runtime.
- You want to improve performance of late-bound objects. You can emit the code necessary to call bound types directly, and then call through your emitted method. Although you cannot perform calls as speedily as with early binding, you will perform better than late binding.
The System.Reflection.Emit namespace provides the classes necessary for a user to create an .exe file on the fly. Its classes allow a compiler or tool to emit metadata and MSIL. So you can create .exe files on your disk on the fly as if you were running the code, saving it, and calling the compiler to compile the code. Mostly you will need this feature and this namespace for your custom script engines and compilers.
The Reflection.Emit namespace has many members you can use for emitting. Here are the two most important ones:
- The AssemblyBuilder class is the starting point for any application that emits code at runtime and has methods for creating dynamic modules.
- The ModuleBuilder class is used as the starting point for adding types such as classes and structures to a dynamic assembly at runtime.
The ILGenerator.OpCodes class, which generates MSIL instructions, includes all the MSIL operation codes in its fields that you will need (operation codes are a portion of a set of operation descriptions that specifies the operation to be performed or the set of operations in a computer). MSIL is the typeless operation code of the base assembly language for the CLR or intermediate language. When you code any C# code and compile it, it is transformed into MSIL first. Then when you invoke an assembly in MSIL, it is converted and executed in the corresponding machine language. The easiest way to learn MSIL is to disassemble simple codes that you have compiled. You can disassemble any compiled .NET code by using ILDasm.exe (the IL Disassembler), one of the .NET SDK utilities. After you have compiled the code in Listing 21.28 and run it on the console, a new file is generated in your current folder called TestAsm.exe. This .exe file prints the message "Hello World" on the console.

Join the conversation! Your thoughts help the community grow.