Freigeben über


Reflection Emit Technology Sample

This sample demonstrates how to use reflection emit. Reflection emit is a run-time feature that allows code to create dynamic assemblies, modules, and types. You can dynamically create instances of these types to use, or you can use reflection emit to generate an assembly and persist it to disk as an executable file or DLL. This sample consists of two executables. The first executable, EmitAssembly.exe, generates a dynamic type and tests it, or it generates a dynamic type and persists it to disk as a DLL. The second executable, TestEmittedAssembly.exe, simply tests the assembly dynamically emitted by the EmitAssembly.exe. The type emitted by EmitAssembly.exe is a simple class called "HelloWorld" whose constructor takes a single string parameter. The type also implements a GetGreeting() method which returns the string passed to the instance constructor.

For information about using the samples, see the following topics:

To build the sample using the command prompt

  1. Open a Command Prompt window and navigate to one of the language-specific subdirectories for the sample.

  2. Type msbuild ReflectionEmitCS.sln or msbuild ReflectionEmitVB.sln, depending on your choice of programming language, at the command line.

  3. Build the testing assembly by compiling the EmitAssembly class. Depending on your programming language choice, type csc /target:library EmitAssembly.cs or vbc /target:library TestEmittedAssembly.vb at the command line. The resulting output file is EmitAssembly.dll.

To build the sample using Visual Studio

  1. Open Windows Explorer and navigate to one of the language-specific subdirectories for the sample.

  2. Double-click the icon for ReflectionEmitCS.sln or ReflectionEmitVB.sln, depending on your choice of programming language, to open the file in Visual Studio.

  3. In the Build menu, select Build Solution.

  4. Build the testing assembly by building the EmitAssembly class. Create a Class Library project using the EmitAssembly.cs or EmitAssembly.vb file and build the project. The resulting output file is EmitAssembly.dll.

To run the sample

  1. Open the Command Prompt window and navigate to the directory that contains the new executable.

  2. Type EmitAssembly.exe, followed by the value you want to pass as a parameter, at the command line.

NoteNote

The sample builds a console application. You must launch and run it in a Command Prompt window to view its output.

Remarks

For more information about reflection emit, see the comments in the source code files.

NoteNote

The MsBuild Tool builds two executable files: EmitAssembly.exe and TestEmittedAssembly.exe. However, to build TestEmittedAssembly.exe, the compiler needs to be able to open the referenced assembly "EmittedAssembly.dll". After building EmitAssembly.exe, the MsBuild Tool executes EmitAssembly with the command line parameter "2" indicating that the sample should emit the dynamic assembly.

The EmitAssembly executable accepts a command line parameter indicating which test you would like to execute. A value of 1 causes the sample to emit the dynamic type into its AppDomain, and test the static members in the sample. A value of 2 causes the sample to emit the dynamic type into an assembly and persist the assembly to disk in the form of a .DLL and a .MOD file. After doing this, you might want to run TestEmittedAssembly.exe or ILDasm.exe to test the emitted code.

For example:

TestEmittedAssembly.exe

ILDasm EmittedModule.mod

A value of 3 causes the sample to emit the dynamic type into an assembly and emit a second dynamic type that tests the code of the first dynamic type.

The sample uses the following technologies and classes:

  • AppDomain The sample uses the AppDomain type to create a dynamic assembly in the sample's AppDomain. It does this by calling the DefineDynamicAssembly method.

  • AssemblyBuilder Used to build a dynamic assembly. It can be used to create an assembly for immediate use, or a dynamic assembly can be persisted to a DLL or EXE file.

  • AssemblyName Used to define the unique identity of an assembly. The sample uses this type in the simplest possible way, by giving the dynamic assembly the text name "EmittedAssembly".

  • ModuleBuilder Used to build a dynamic module in the dynamic assembly.

  • TypeBuilder Used to build a type dynamically. This type is derived from Type. From it you can request instances of the FieldBuilder, ConstructorBuilder, and MethodBuilder types. Using these types the sample generates a complete type.

  • FieldBuilder Used to create a field in a dynamic type.

  • ConstructorBuilder Used to define a constructor in a dynamic type.

  • MethodBuilder Used to define a method in a dynamic type.

  • ILGenerator Both MethodBuilder and ConstructorBuilder implement the GetILGenerator method. These methods return an instance of the ILGenerator type, which is used to generate Microsoft Intermediate Language (MSIL) code dynamically for a type.

  • Thread Used to get an instance of the AppDomain type for the current thread.

See Also

Reference

AppDomain
AssemblyBuilder
AssemblyBuilderAccess
AssemblyName
ConstructorBuilder
FieldBuilder
ILGenerator
MethodAttributes
MethodBuilder
MethodInfo
ModuleBuilder
ResolveEventHandler
System.IO
System.Reflection
System.Reflection.Emit
System.Threading
TypeBuilder

Other Resources

Reflection
Emitting Dynamic Methods and Assemblies
Dynamic Source Code Generation and Compilation
Handling and Raising Events
Managed and Unmanaged Events